0

我创建了这个 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);
                    }

                }

            }

我究竟做错了什么??

4

1 回答 1

1

元素的顺序是错误的。您已将订单定义为line-text,box-textrelative-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>
于 2013-06-11T15:27:57.843 回答