我创建了这个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);
}
}
}
我做错了什么?
答案 0 :(得分:1)
元素的顺序是错误的。您已将订单定义为line-text
,box-text
和relative-text
,而不是示例line-text
,relative-text
,box-text
因此要么将模板xml更改为:
<?xml version='1.0'?>
<template-content output-directory='D:\\output'>
<line-text name='a' />
<box-text name='c' x='1' y='2' width='2' height='2' />
<relative-text name='b' flow='above' amount='1'/>
</template-content>
或在您的架构中使用<xs:all />
代替<xs:sequence />
<xs:all>
<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:all>
修改强>
我想我误读了你的架构。使用<xs:all />
它将允许每个元素中的一个按任意顺序排列。但是从你的架构看起来你想要任意数量的元素。为此,您必须使用<xs:choice maxOccurs="unbound" />
。
<xs:choice maxOccurs="unbound">
<xs:element name="line-text" type="LineText" />
<xs:element name="box-text" type="BoxText" />
<xs:element name="relative-text" type="RelativeText" />
</xs:choice>