我正在尝试将 cXML 字符串反序列化为 C# 类。但是,在反序列化时,除了要反序列化的根元素之外,我似乎什么也得不到。如何反序列化 XML 文档似乎提供了最大的帮助,并且使用该示例我设法反序列化根 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
.
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>
cXML 类由xsd.exe
(分段)生成
[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;
}
}
}