我创建了这个 xsd 架构:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:complexType name="RelativeText">
<xs:attribute name="name" type="stringtype" use="required"/>
<xs:attribute name="flow" type="stringtype" use="required"/>
<xs:attribute name="amount" type="inttype" use="required"/>
</xs:complexType>
<xs:complexType name="LineText">
<xs:attribute name="name" type="stringtype" use="required"/>
</xs:complexType>
<xs:complexType name="BoxText">
<xs:attribute name="width" type="dectype" use="required" />
<xs:attribute name="height" type="dectype" use="required" />
<xs:attribute name="x" type="dectype" use="required" />
<xs:attribute name="y" type="dectype" use="required" />
</xs:complexType>
<xs:complexType name="templatecontenttype">
<xs:sequence>
<xs:element name="line-text" type="LineText" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="box-text" type="BoxText" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="relative-text" type="RelativeText" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="output-directory" type="stringtype" use="required"/>
</xs:complexType>
<xs:element name="template-content" type="templatecontenttype" />
</xs:schema>
对于这个 xml :
<?xml version='1.0'?>
<template-content output-directory='D:\\output'>
<line-text name='a' />
<relative-text name='b' flow='above' amount='1'/>
<box-text name='c' x='1' y='2' width='2' height='2' />
</template-content>
它说:
行:5,位置:2 “元素 'template-content' 具有无效的子元素 'box-tex t'。预期的可能元素列表:'relative-text'。”
C#代码:
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
XmlReaderSettings rs = new XmlReaderSettings();
rs.ValidationType = ValidationType.Schema;
rs.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(rs_ValidationEventHandler);
rs.Schemas.Add(null, xsdFilePath);
rs.CloseInput = true;
rs.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
StringReader r = new StringReader(xmlString);
using (XmlReader reader = XmlReader.Create(r, rs))
{
// Parse the file and display each of the nodes.
while (reader.Read())
{
try
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
{
if (reader.Name == "relative-text")
{
//Console.WriteLine("we found custom-text");
//Console.WriteLine(reader["name"]);
//Console.WriteLine(reader["flow"]);
//Console.WriteLine(reader["amount"]);
}
else if (reader.Name == "line-text")
{
// Console.WriteLine(reader["names"]);
}
else if (reader.Name == "box-text")
{
//Console.WriteLine("x" + reader["x"]);
//Console.WriteLine("y" + reader["y"]);
//Console.WriteLine("width" + reader["width"]);
//Console.WriteLine("height" + reader["height"]);
}
}
break;
case XmlNodeType.Text:
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
我究竟做错了什么??