xsd的unique验证
由于 xpath 1.0 中没有默认空间的概念,没有前缀的元素都将被视为无名称空间中的元素。所以无法进行唯一性约束。
xsd 文档 user.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.user.com" xmlns:us="http://www.user.com"
attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Users">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" maxOccurs="unbounded" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="nameUnique">
<xs:selector xpath="us:name" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
</xs:schema>
xml文件 user.xml:
<?xml version="1.0" encoding="utf-8"?>
<Users>
<name>aa</name>
<name>bb</name>
<name>aa</name>
</Users>
本来xml文件应该:
<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.user.com"
xsi:schemaLocation="http://www.user.com user.xsd">
<name>aa</name>
<name>bb</name>
<name>aa</name>
</Users>
给xml文件添加属性:
XmlElement root = doc.DocumentElement;
root.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xmlns", @"http://www.user.com");
root.SetAttribute("xsi:schemaLocation", @"http://www.user.com user.xsd");
加上属性后的文档
<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.user.com"
schemaLocation="http://www.user.com user.xsd">
<name>aa</name>
<name>bb</name>
<name>aa</name>
</Users>
提示错误:未声明“schemaLocation”属性。
其中 schemaLocation 前面没有 xsi?。
下面的代码可以验证通过
XmlElement root = doc.DocumentElement;
//root.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xmlns", @"http://www.user.com");
//root.SetAttribute("xsi:schemaLocation", @"http://www.user.com user.xsd");
TextReader reader = new StringReader(doc.InnerXml);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, xsdPath);
XmlReader xmlReader = XmlReader.Create(reader, settings);
try
{
while (xmlReader.Read())
{
}
}
catch (Exception ce)
{
throw new Exception(ce.Message);
}
提示错误:“http://www.user.com:nameUnique”键或唯一标识约束中包含重复的键序列“aa”。
xsd 文档 user.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.user.com" xmlns:us="http://www.user.com"
attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Users">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" maxOccurs="unbounded" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="nameUnique">
<xs:selector xpath="us:name" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
</xs:schema>
xml文件 user.xml:
<?xml version="1.0" encoding="utf-8"?>
<Users>
<name>aa</name>
<name>bb</name>
<name>aa</name>
</Users>
本来xml文件应该:
<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.user.com"
xsi:schemaLocation="http://www.user.com user.xsd">
<name>aa</name>
<name>bb</name>
<name>aa</name>
</Users>
给xml文件添加属性:
XmlElement root = doc.DocumentElement;
root.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xmlns", @"http://www.user.com");
root.SetAttribute("xsi:schemaLocation", @"http://www.user.com user.xsd");
加上属性后的文档
<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.user.com"
schemaLocation="http://www.user.com user.xsd">
<name>aa</name>
<name>bb</name>
<name>aa</name>
</Users>
提示错误:未声明“schemaLocation”属性。
其中 schemaLocation 前面没有 xsi?。
下面的代码可以验证通过
XmlElement root = doc.DocumentElement;
//root.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xmlns", @"http://www.user.com");
//root.SetAttribute("xsi:schemaLocation", @"http://www.user.com user.xsd");
TextReader reader = new StringReader(doc.InnerXml);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, xsdPath);
XmlReader xmlReader = XmlReader.Create(reader, settings);
try
{
while (xmlReader.Read())
{
}
}
catch (Exception ce)
{
throw new Exception(ce.Message);
}
提示错误:“http://www.user.com:nameUnique”键或唯一标识约束中包含重复的键序列“aa”。