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