如何反序列化 Transunion XML?
这是我的尝试,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您发布的 XML 正是您收到的内容,则会出现错误,因为 XML 文档不正确:
不是有效的 XML 声明标记,它应该是这样的:
Assuming the XML you posted is exactly what you receive, the error occurs because the XML document is incorrect:
Is not a valid XML declaration tag, it should be something like that: