缺少XML子元素?

时间:2014-04-28 19:25:39

标签: c# xml xsd xmlwriter

我正在练习学校考试,我不确定我是否正确这样做。考试使用给定的架构创建一个xml文件。我错过了一个孩子元素,还是我只是做错了什么。感谢

C#

   writer.WriteStartDocument();

            writer.WriteStartElement("LoanID");
            writer.WriteString("9980001140");

            writer.WriteStartElement("LoanAmount");
            writer.WriteString("150000");

            writer.WriteStartElement("BORROWER");
            writer.WriteString("John Smith");

            writer.WriteStartElement("FullName");
            writer.WriteString("Johnny Smoth");
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();

模式

  <xs:complexType name="LOANDATA">
    <xs:sequence>
      <xs:element name="LoanID" nillable="false" type="xs:string"></xs:element>
      <xs:element name="LoanAmount" nillable="false" type="xs:double"></xs:element>
      <xs:element name="BORROWERS" maxOccurs="1" nillable="false" type="BORROWERS">      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="BORROWERS">
    <xs:sequence>
      <xs:element name="BORROWER" minOccurs="1" nillable="false" type="BORROWER"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="BORROWER">
    <xs:sequence>
      <xs:element name="FullName" nillable="false" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>

输出

<LoanID>
  9980001140
 <LoanAmount>
   150000
 <BORROWER>
  John Smith
 <FullName>Johnny Smoth</FullName>
 </BORROWER>
 </LoanAmount>
  </LoanID>

2 个答案:

答案 0 :(得分:1)

对于初学者来说,你的“架构”实际上并不是一个有效的架构,它只是定义一些复杂类型的一个片段。

完整模式看起来更像下图,您将其声明为模式并定义使用的名称空间。

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://SomeNameSpace.LoanData" targetNamespace="http://SomeNameSpace.LoanData" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="LoanData">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="LOANDATA" />
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="LOANDATA">
    <xs:sequence>
      <xs:element name="LoanID" nillable="false" type="xs:string" />
      <xs:element name="LoanAmount" nillable="false" type="xs:double" />
      <xs:element name="BORROWERS" maxOccurs="1" nillable="false" type="BORROWERS" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="BORROWERS">
    <xs:sequence>
      <xs:element name="BORROWER" minOccurs="1" nillable="false" type="BORROWER" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="BORROWER">
    <xs:sequence>
      <xs:element name="FullName" nillable="false" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

其次,最好使用xmlserializer

对此进行编码

使用xsd.exe创建一个类(例如xsd.exe / c loandata.xsd),然后让您的类创建一个适合填充的实例。

using System.IO;
using System.Xml.Serialization;
...
...
        LoanData loan = new LoanData();
        loan.LoanID = "9980001140";
        loan.LoanAmount = 150000;

        BORROWER borrower = new BORROWER();
        borrower.FullName = "Johnny Smoth";

        loan.BORROWERS = new BORROWERS();
        loan.BORROWERS.BORROWER = new BORROWER();

        loan.BORROWERS.BORROWER = borrower;

        TextWriter writer = new StreamWriter("C:\\Test\\LoanData.xml");

        XmlSerializer ser = new XmlSerializer(typeof(LoanData));
        ser.Serialize(writer, loan);
        writer.Close();

输出

<?xml version="1.0" encoding="utf-8"?>
<LoanData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://SomeNameSpace.LoanData">
  <LoanID xmlns="">9980001140</LoanID>
  <LoanAmount xmlns="">150000</LoanAmount>
  <BORROWERS xmlns="">
    <BORROWER>
      <FullName>Johnny Smoth</FullName>
    </BORROWER>
  </BORROWERS>
</LoanData>

如果您确实想使用XMLWriter,那么它应该是

        writer.WriteStartDocument();

        writer.WriteStartElement("LoanData"); // Have a root node

        writer.WriteStartElement("LoanID");
        writer.WriteString("9980001140");
        writer.WriteEndElement();

        writer.WriteStartElement("LoanAmount");
        writer.WriteString("150000");
        writer.WriteEndElement();

        writer.WriteStartElement("BORROWERS");
        writer.WriteStartElement("BORROWER");

        writer.WriteStartElement("FullName");
        writer.WriteString("Johnny Smoth");
        writer.WriteEndElement(); // FullName

        writer.WriteEndElement(); // BORROWER
        writer.WriteEndElement(); // BORROWERS
        writer.WriteEndElement(); // LoanData

        writer.WriteEndDocument();
        writer.Close();

其中有

的输出
<?xml version="1.0" encoding="utf-8"?>
<LoanData>
  <LoanID>9980001140</LoanID>
  <LoanAmount>150000</LoanAmount>
  <BORROWERS>
    <BORROWER>
      <FullName>Johnny Smoth</FullName>
    </BORROWER>
  </BORROWERS>
</LoanData>

答案 1 :(得分:0)

您已拨打XmlWriter.StartElement(string) 4次,但XmlWriter.EndElement()仅拨打一次。

每次XmlWriter.WriteEnd*()来电都需要匹配XmlWriter.WriteStart*()次来电。