如何反序列化 Transunion XML?

发布于 2024-09-15 19:28:22 字数 3199 浏览 4 评论 0原文

这是我的尝试,xsd 和从 xsd.exe 创建的类

运行我的代码,出现错误“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">...

类片段:

[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);

Here is my attempt, xsd and classes created from xsd.exe

Running my code I get error "There is an error in XML document (1, 2)." Inner exception {" was not expected."}

Any help would be greatly appreciated.

Thanks!

XML Snippet:

 <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 Snippet:

<?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;
        }
    }
}

Code Snippet:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-09-22 19:28:23

假设您发布的 XML 正是您收到的内容,则会出现错误,因为 XML 文档不正确:

<xml>

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

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

Assuming the XML you posted is exactly what you receive, the error occurs because the XML document is incorrect:

<xml>

Is not a valid XML declaration tag, it should be something like that:

<?xml version="1.0" encoding="utf-8"?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文