生成 XSD 时 XML 文字中的 LINQ 出现问题
Issue with LINQ inside XML Literals when generating XSD
我正在根据 this answer 在 XSD 中创建一个枚举。这是我的目标,供参考:
<xs:simpleType name="color" final="restriction" >
<xs:restriction base="xs:string">
<xs:enumeration value="green" />
<xs:enumeration value="red" />
<xs:enumeration value="blue" />
</xs:restriction>
</xs:simpleType>
<xs:element name="SomeElement">
<xs:complexType>
<xs:sequence>
<xs:element name="Color" type="color" />
</xs:sequence>
</xs:complexType>
</xs:element>
枚举来自一个IEnumerable(Of String)
,我想从中直接生成一系列这些元素<xs:enumeration value="value1" />
每当我 运行 我的代码时,它都会正确地创建 xml 直到使用 LINQ,它开始将 > 和 < 转义为 >
和 <
.
输出:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="Instruments">
<xs:simpleType name="InstrumentName" final="restriction">
<xs:restriction base="xs:string"><xs:enumeration value="AccuvimDL"/><xs:enumeration value="AccuvimDLSignal"/><xs:enumeration value="AlignmentFormBase" ...
我的代码:
Private Sub saveXsd(names As IEnumerable(Of String), typeName As String, typeType As String)
Dim xsd = <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Instruments" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name=<%= typeType %> final="restriction">
<xs:restriction base="xs:string">
<%= From name In names
Select $"<xs:enumeration value=""{name}""/>" %>
</xs:restriction>
</xs:simpleType>
<xs:element name="SomeElement">
<xs:complexType>
<xs:sequence>
<xs:element name=<%= typeName %> type=<%= typeType %>/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xsd.Save("xsd.xsd")
End Function
我也在 LINQ 部分尝试了这种语法
<%= From name In names Select <xs:enumeration value=<%= name %>/> %>
但无法识别 <xs:
命名空间,因为它不再是 xml 文字的一部分,无法编译。
我想知道这是否可行。我觉得VB.Net的xml文字功能很酷,希望能用上。
编辑:
写这个问题一定让我脑子里有些东西松动了。我找到了一个解决方案,虽然不是很理想。
如果我添加命名空间,第二种 LINQ 方法将起作用
<%= From name In names
Select <xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value=<%= name %>/> %>
然而,我最终在每个元素中都有名称空间。我想 xsd...
没问题
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="Instruments">
<xs:simpleType name="InstrumentName" final="restriction">
<xs:restriction base="xs:string">
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="AccuvimDL" />
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="AccuvimDLSignal" />
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="AlignmentFormBase" />
...
仍然接受其他建议
抱歉不熟悉 VB.Net XML 文字,所以我很好奇。
看来您需要导入
Imports <xmlns:xs="http://www.w3.org/2001/XMLSchema">
Module Module1
Sub Main()
Dim xsd = <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Instruments" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name=<%= "test" %> final="restriction">
<xs:restriction base="xs:string">
<%= From name In names
Select <xs:enumeration value=" {name}"/> %>
</xs:restriction>
</xs:simpleType>
</xs:schema>
xsd.Save("xsd.xsd")
End Sub
End Module
我正在根据 this answer 在 XSD 中创建一个枚举。这是我的目标,供参考:
<xs:simpleType name="color" final="restriction" >
<xs:restriction base="xs:string">
<xs:enumeration value="green" />
<xs:enumeration value="red" />
<xs:enumeration value="blue" />
</xs:restriction>
</xs:simpleType>
<xs:element name="SomeElement">
<xs:complexType>
<xs:sequence>
<xs:element name="Color" type="color" />
</xs:sequence>
</xs:complexType>
</xs:element>
枚举来自一个IEnumerable(Of String)
,我想从中直接生成一系列这些元素<xs:enumeration value="value1" />
每当我 运行 我的代码时,它都会正确地创建 xml 直到使用 LINQ,它开始将 > 和 < 转义为 >
和 <
.
输出:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="Instruments">
<xs:simpleType name="InstrumentName" final="restriction">
<xs:restriction base="xs:string"><xs:enumeration value="AccuvimDL"/><xs:enumeration value="AccuvimDLSignal"/><xs:enumeration value="AlignmentFormBase" ...
我的代码:
Private Sub saveXsd(names As IEnumerable(Of String), typeName As String, typeType As String)
Dim xsd = <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Instruments" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name=<%= typeType %> final="restriction">
<xs:restriction base="xs:string">
<%= From name In names
Select $"<xs:enumeration value=""{name}""/>" %>
</xs:restriction>
</xs:simpleType>
<xs:element name="SomeElement">
<xs:complexType>
<xs:sequence>
<xs:element name=<%= typeName %> type=<%= typeType %>/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xsd.Save("xsd.xsd")
End Function
我也在 LINQ 部分尝试了这种语法
<%= From name In names Select <xs:enumeration value=<%= name %>/> %>
但无法识别 <xs:
命名空间,因为它不再是 xml 文字的一部分,无法编译。
我想知道这是否可行。我觉得VB.Net的xml文字功能很酷,希望能用上。
编辑:
写这个问题一定让我脑子里有些东西松动了。我找到了一个解决方案,虽然不是很理想。
如果我添加命名空间,第二种 LINQ 方法将起作用
<%= From name In names
Select <xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value=<%= name %>/> %>
然而,我最终在每个元素中都有名称空间。我想 xsd...
没问题<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="Instruments">
<xs:simpleType name="InstrumentName" final="restriction">
<xs:restriction base="xs:string">
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="AccuvimDL" />
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="AccuvimDLSignal" />
<xs:enumeration xmlns:xs="http://www.w3.org/2001/XMLSchema" value="AlignmentFormBase" />
...
仍然接受其他建议
抱歉不熟悉 VB.Net XML 文字,所以我很好奇。
看来您需要导入
Imports <xmlns:xs="http://www.w3.org/2001/XMLSchema">
Module Module1
Sub Main()
Dim xsd = <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Instruments" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name=<%= "test" %> final="restriction">
<xs:restriction base="xs:string">
<%= From name In names
Select <xs:enumeration value=" {name}"/> %>
</xs:restriction>
</xs:simpleType>
</xs:schema>
xsd.Save("xsd.xsd")
End Sub
End Module