你如何反序化Transunion XML?

时间:2010-08-25 15:11:39

标签: c# .net partial-classes xsd.exe xml-deserialization

这是我的尝试,xsd和从xsd.exe创建的类<​​/ p>

运行我的代码我收到错误“XML文档中存在错误(1,2)。”内部异常{“不期望。”}

非常感谢任何帮助。

谢谢!

XML代码段:

 <xml>
  <creditBureau xmlns="http://www.transunion.com/namespace"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <document>response</document>
    <version>2.2</version>
    <transactionControl>
      <userRefNumber>260907</userRefNumber>
      <subscriber>
        <industryCode>P</industryCode>
        <memberCode>04784547</memberCode>
        <inquirySubscriberPrefixCode>1527</inquirySubscriberPrefixCode>
      </subscriber>...

XSD片段:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://www.transunion.com/namespace" 
           attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           targetNamespace="http://www.transunion.com/namespace" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="creditBureau">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="document" type="xs:string" />
        <xs:element name="version" type="xs:decimal" />
        <xs:element name="transactionControl">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="userRefNumber" type="xs:unsignedInt" />
              <xs:element name="subscriber">...

Class Snippet:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.transunion.com/namespace")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.transunion.com/namespace", IsNullable=false)]
public partial class creditBureau {
    private string documentField;
    private decimal versionField;
    private creditBureauTransactionControl transactionControlField;
    private creditBureauProduct productField;

    public string document {
        get {
            return this.documentField;
        }
        set {
            this.documentField = value;
        }
    }

    public decimal version {
        get {
            return this.versionField;
        }
        set {
            this.versionField = value;
        }
    }

    public creditBureauTransactionControl transactionControl {
        get {
            return this.transactionControlField;
        }
        set {
            this.transactionControlField = value;
        }
    }

    public creditBureauProduct product {
        get {
            return this.productField;
        }
        set {
            this.productField = value;
        }
    }
}

代码段:

    FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
    XmlSerializer x = new XmlSerializer(typeof(creditBureau));
    creditBureau c = (creditBureau)x.Deserialize(fs);

1 个答案:

答案 0 :(得分:1)

假设您发布的XML与您收到的完全一致,则会发生错误,因为XML文档不正确:

<xml>

不是有效的XML声明标记,它应该是这样的:

<?xml version="1.0" encoding="utf-8"?>