[英]C# - Get part of XML to be validated by XSD using attribute value
I need to validate column values before inserting them in table. 我需要先验证列值,然后再将它们插入表中。 I have written a common XSD for all tables and my XML looks like this.
我为所有表编写了通用的XSD,我的XML看起来像这样。
<root>
<table name="User">
<column>UserId</column>
<column>Name</column>
<column>RoleId</column>
</table>
<table name="Role">
<column>RoleId</column>
<column>RoleName</column>
</table>
</root>
And the xsd like this: 像这样的xsd:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="table" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="column" maxOccurs="unbounded" minOccurs="1"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But my condition is at one time I will insert in either 'User' or 'Role' table. 但我的条件是,我会一次插入“用户”或“角色”表。 So I need to validate only that part of the XML.
因此,我只需要验证XML的那部分。 Is it possible to access that part in c# using the attribute value which is my table name?
是否可以使用属性值(我的表名称)访问c#中的该部分?
The XSD should be in below format; XSD应该采用以下格式;
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Message">
<xs:annotation>
<xs:documentation> </xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:choice>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="table" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="column" maxOccurs="unbounded" minOccurs="1"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>`
</xs:complexType>
</xs:element>
</xs:schema>
Under choice tag you can add your individual elements, if you have more than one elements. 如果您有多个元素,则可以在选择标签下添加各个元素。
` `
I found the answer... I pass the path along with the attribute value. 我找到了答案...我将路径与属性值一起传递。
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString);
XmlNodeList xnList = xml1.SelectNodes("/root/table[@name='Role']");
foreach (XmlNode xn in xnList)
{
string colName = xn["column"].InnerText;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.