将cXML反序列化为C#类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将cXML反序列化为C#类相关的知识,希望对你有一定的参考价值。
我正在尝试将cXML字符串反序列化为C#类。但是,在反序列化时,除了根元素之外,我似乎什么也无法得到。 How to Deserialize XML document似乎提供了最大的帮助,在该示例中,我设法对根cXML标记进行反序列化,但这不适用于嵌套对象PunchOutSetupRequest
,这是我真正需要反序列化的对象。
我的问题,在向您展示代码墙之前,如何在不更改cXML.cs
文件的情况下,将这些对象正确地反序列化为下面定义的cXML类?由于这是一个标准,除非绝对必要,否则我不想编辑它。我的代码是公平的游戏。预先感谢您的任何帮助,因为我知道该主题在SO的各个角落都有涉及。
我有用于cXML的XSD文件,我已经使用xsd.exe
工具将其转换为C#类。为了避免稀释问题,我不会在此处粘贴整个架构,但是如果需要更完整的图片,可以转到cxml.org。
我的cXML代码仅反序列化属性,不包含cXML中定义的Header或Request对象:
using (TextReader reader = new StringReader(text))
{
try
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "cXML";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(cXML), xRoot);
cxml = (cXML)serializer.Deserialize(reader);
}
catch (Exception ex)
{
string r = "";
}
}
我的PunchOutSetupRequest
反序列化代码。尽管我将xRoot
元素设置为PunchOutSetupRequest
,但当将cXML视为根节点时,这将引发错误。
using (TextReader reader = new StringReader(text)) { try { XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "PunchOutSetupRequest"; xRoot.IsNullable = true; var serializer = new XmlSerializer(typeof(PunchOutSetupRequest), xRoot); PunchOutSetupRequest request; request = (PunchOutSetupRequest)serializer.Deserialize(reader); } catch (Exception ex) { string r = ""; } }
文本变量值(已删除数据):
<?xml version = '1.0' encoding = 'UTF-8'?> <!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.1.007/cXML.dtd"> <cXML version="1.1.007" xml:lang="en-US" payloadID="" timestamp="2016-01- 04T03:21:32-05:00"> <Header> <From> <Credential domain=""> <Identity></Identity> </Credential> </From> <To> <Credential domain=""> <Identity></Identity> </Credential> </To> <Sender> <Credential domain=""> <Identity></Identity> <SharedSecret></SharedSecret> </Credential> <UserAgent></UserAgent> </Sender> </Header> <Request> <PunchOutSetupRequest operation="create"> <BuyerCookie></BuyerCookie> <Extrinsic name="User"></Extrinsic> <BrowserFormPost> <URL></URL> </BrowserFormPost> <Contact> <Name xml:lang="en-US"></Name> <Email></Email> </Contact> <SupplierSetup> <URL></URL> </SupplierSetup> </PunchOutSetupRequest> </Request> </cXML>
xsd.exe
生成的cXML类(分段)
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/cXML")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/cXML", IsNullable = false)]
public partial class cXML
{
private object[] itemsField;
private string versionField;
private string payloadIDField;
private string timestampField;
private string langField;
public cXML()
{
this.versionField = "1.1.010";
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Header", typeof(Header))]
[System.Xml.Serialization.XmlElementAttribute("Message", typeof(Message))]
[System.Xml.Serialization.XmlElementAttribute("Request", typeof(Request))]
[System.Xml.Serialization.XmlElementAttribute("Response", typeof(Response))]
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute("1.1.010")]
public string version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string payloadID
{
get
{
return this.payloadIDField;
}
set
{
this.payloadIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string timestamp
{
get
{
return this.timestampField;
}
set
{
this.timestampField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
public string lang
{
get
{
return this.langField;
}
set
{
this.langField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/cXML")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/cXML", IsNullable = false)]
public partial class PunchOutSetupRequest
{
private BuyerCookie buyerCookieField;
private Extrinsic[] extrinsicField;
private BrowserFormPost browserFormPostField;
private Contact[] contactField;
private SupplierSetup supplierSetupField;
private ShipTo shipToField;
private SelectedItem selectedItemField;
private ItemOut[] itemOutField;
private PunchOutSetupRequestOperation operationField;
/// <remarks/>
public BuyerCookie BuyerCookie
{
get
{
return this.buyerCookieField;
}
set
{
this.buyerCookieField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Extrinsic")]
public Extrinsic[] Extrinsic
{
get
{
return this.extrinsicField;
}
set
{
this.extrinsicField = value;
}
}
/// <remarks/>
public BrowserFormPost BrowserFormPost
{
get
{
return this.browserFormPostField;
}
set
{
this.browserFormPostField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Contact")]
public Contact[] Contact
{
get
{
return this.contactField;
}
set
{
this.contactField = value;
}
}
/// <remarks/>
public SupplierSetup SupplierSetup
{
get
{
return this.supplierSetupField;
}
set
{
this.supplierSetupField = value;
}
}
/// <remarks/>
public ShipTo ShipTo
{
get
{
return this.shipToField;
}
set
{
this.shipToField = value;
}
}
/// <remarks/>
public SelectedItem SelectedItem
{
get
{
return this.selectedItemField;
}
set
{
this.selectedItemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ItemOut")]
public ItemOut[] ItemOut
{
get
{
return this.itemOutField;
}
set
{
this.itemOutField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public PunchOutSetupRequestOperation operation
{
get
{
return this.operationField;
}
set
{
this.operationField = value;
}
}
}
我正在尝试将cXML字符串反序列化为C#类。但是,在反序列化时,除了根元素之外,我似乎什么也无法得到。似乎如何反序列化XML文档...
TL; DR;
以上是关于将cXML反序列化为C#类的主要内容,如果未能解决你的问题,请参考以下文章
使用 JsonConvert.DeserializeObject 将 Json 反序列化为 C# POCO 类