复杂的XML反序列化
本文关键字:反序列化 XML 复杂 | 更新日期: 2023-09-27 18:14:23
我正在尝试反序列化一个复杂的XML文件。我有我的主类结构,所以它得到所有的信息在第一个子节点,我甚至有它,所以我可以获得ClientName
,这是两层深。然而,除此之外的任何东西似乎都不起作用。我得到一个计数为1的List项,但列表中没有任何信息。
我的OrderTaxes
和OrderTransactions
列表返回Count = 1
,但所有字段都为空。
我肯定这是我的类结构的问题,任何帮助纠正这个问题将是非常感激的。
下面是XML:
<OrderDetail>
<MessageTypeCode>82540</MessageTypeCode>
<OrderDetailId>59339463</OrderDetailId>
<ClientInfo>
<ClientName>LenderName will appear here</ClientName>
</ClientInfo>
<OrderTaxes>
<OrderTax>
<TaxId>9202225</TaxId>
</OrderTax>
</OrderTaxes>
<OrderTransactions>
<OrderTransaction>
<LoanAmount/>
<Title>
<TitleVendors>
<TitleVendor>
<VendorInstructions>blah blah blah blah .</VendorInstructions>
<VendorServices>
<TitleVendorService>
<TitleVendorServiceId>6615159</TitleVendorServiceId>
<ServiceCode>1OWNER</ServiceCode>
<CustomVendorInstructions>blah blah blah blah blah </CustomVendorInstructions>
</TitleVendorService>
</VendorServices>
</TitleVendor>
</TitleVendors>
</Title>
</OrderTransaction>
</OrderTransactions>
</OrderDetail>
这里是类:
namespace TSIxmlParser
{
[XmlRoot("OrderDetail")]
public class OrderData
{
[XmlElement("MessageTypeCode")]
public string MessageTypeCode { get; set; }
[XmlElement("OrderDetailId")]
public string OrderNumber { get; set; }
[XmlElement("ClientInfo")]
public List<ClientInfo> ClientInfos = new List<ClientInfo>();
[XmlArray("OrderTaxes")]
[XmlArrayItem("OrderTax")]
public List<OrderTax> OrderTaxes = new List<OrderTax>();
[XmlArray("OrderTransactions")]
[XmlArrayItem("OrderTransaction")]
public List<OrderTransaction> OrderTransactions = new List<OrderTransaction>();
}
public class ClientInfo
{
[XmlElement("ClientName")]
public string ClientName { get; set; }
}
public class OrderTax
{
[XmlElement("TaxId")]
public string TaxId { get; set; }
}
public class OrderTransaction
{
[XmlElement("LoanAmount")]
public string LoanAmount { get; set; }
[XmlArray("Title")]
[XmlArrayItem("TitleVendors")]
public List<Title> Titles { get; set; }
}
public class Title
{
[XmlArrayItem("TitleVendors")]
public List<TitleVendors> TitleVendors { get; set; }
}
public class TitleVendors
{
[XmlArray("TitleVendor")]
public List<TitleVendor> TitleVendor { get; set; }
}
public class TitleVendor
{
[XmlElement("VendorInstructions")]
public string VendorInstructions { get; set; }
[XmlArray("VendorServices")]
[XmlArrayItem("TitleVendorService")]
public List<TitleVendorService> VendorServices { get; set; }
}
public class TitleVendorService
{
[XmlElement("TitleVendorServiceId")]
public string TitleVendorServiceId { get; set; }
[XmlElement("ServiceCode")]
public string ServiceCode { get; set; }
[XmlElement("CustomVendorInstructions")]
public string CustomVendorInstructions { get; set; }
}
}
. net SDK提供了一个很好的工具。XML时间表定义工具(csd.exe)。
更多信息:http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx
我使用您的XML运行了一个示例,以展示它是如何工作的。
- 首先,我把你的XML保存到我的桌面一个名为"sample.xml"的文件 我以管理员身份打开VS 2012命令提示符
在命令提示符中,我导航到安装SDK的目录。对我来说,它安装在这里(可能不同的操作系统或VS版本):
C:'Program Files (x86)'Microsoft SDKs'Windows'v8.1A'bin'NETFX 4.5.1 Tools
我运行以下命令为XML创建XSD。(预计样本。XSD出现在桌面上)
xsd "c:'users'glenn'desktop'sample.xml" /outputdir:"c:'users'glenn'desktop"
我运行以下命令从生成的XSD文件创建一个CSharp文件。
xsd "c:'users'glenn'desktop'sample.xsd" /classes /outputdir:"c:'users'glenn'desktop"
除去注释,这是为您的XML模式定制生成的CSharp文件。还有其他选项可以设置名称空间和类型名。详情请参阅上面的文章。
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class OrderDetail {
private string messageTypeCodeField;
private string orderDetailIdField;
private OrderDetailClientInfo[] clientInfoField;
private OrderDetailOrderTaxesOrderTax[][] orderTaxesField;
private OrderDetailOrderTransactionsOrderTransaction[][] orderTransactionsField;
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MessageTypeCode {
get {
return this.messageTypeCodeField;
}
set {
this.messageTypeCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OrderDetailId {
get {
return this.orderDetailIdField;
}
set {
this.orderDetailIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ClientInfo", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public OrderDetailClientInfo[] ClientInfo {
get {
return this.clientInfoField;
}
set {
this.clientInfoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("OrderTax", typeof(OrderDetailOrderTaxesOrderTax), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public OrderDetailOrderTaxesOrderTax[][] OrderTaxes {
get {
return this.orderTaxesField;
}
set {
this.orderTaxesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("OrderTransaction", typeof(OrderDetailOrderTransactionsOrderTransaction), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public OrderDetailOrderTransactionsOrderTransaction[][] OrderTransactions {
get {
return this.orderTransactionsField;
}
set {
this.orderTransactionsField = 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)]
public partial class OrderDetailClientInfo {
private string clientNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ClientName {
get {
return this.clientNameField;
}
set {
this.clientNameField = 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)]
public partial class OrderDetailOrderTaxesOrderTax {
private string taxIdField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TaxId {
get {
return this.taxIdField;
}
set {
this.taxIdField = 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)]
public partial class OrderDetailOrderTransactionsOrderTransaction {
private string loanAmountField;
private OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor[][][] titleField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LoanAmount {
get {
return this.loanAmountField;
}
set {
this.loanAmountField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("TitleVendors", typeof(OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("TitleVendor", typeof(OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false, NestingLevel=1)]
public OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor[][][] Title {
get {
return this.titleField;
}
set {
this.titleField = 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)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor {
private string vendorInstructionsField;
private OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService[][] vendorServicesField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string VendorInstructions {
get {
return this.vendorInstructionsField;
}
set {
this.vendorInstructionsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("TitleVendorService", typeof(OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService[][] VendorServices {
get {
return this.vendorServicesField;
}
set {
this.vendorServicesField = 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)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService {
private string titleVendorServiceIdField;
private string serviceCodeField;
private string customVendorInstructionsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TitleVendorServiceId {
get {
return this.titleVendorServiceIdField;
}
set {
this.titleVendorServiceIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ServiceCode {
get {
return this.serviceCodeField;
}
set {
this.serviceCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CustomVendorInstructions {
get {
return this.customVendorInstructionsField;
}
set {
this.customVendorInstructionsField = 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)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class NewDataSet {
private OrderDetail[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("OrderDetail")]
public OrderDetail[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
希望这对你有帮助!
尝试在相应的属性上使用XmlArray和XmlArrayItem。
[XmlArray("OrderTaxes")]
[XmlArrayItem("OrderTax")]
public List<OrderTax> OrderTaxes = new List<OrderTax>();
和
[XmlArray("OrderTransactions")]
[XmlArrayItem("OrderTransaction")]
public List<OrderTransaction> OrderTransactions = new List<OrderTransaction>();
这样序列化程序将知道这些将被视为集合,并将知道如何查找特定的项。
除了这两个之外,我想说的是,无论在哪里定义元素列表,都应该使用这种方法。titlevendor很可能也需要类似的东西
嗯,我不确定您是否想要这个(反序列化XML的原始转储):
{
MessageTypeCode: 82540,
OrderDetailId: 59339463,
ClientInfo:
{
ClientName: LenderName will appear here
},
OrderTaxes:
{
OrderTax:
{
TaxId: 9202225
}
},
OrderTransactions:
{
OrderTransaction:
{
LoanAmount: {},
Title:
{
TitleVendors:
{
TitleVendor:
{
VendorInstructions: blah blah blah blah .,
VendorServices:
{
TitleVendorService:
{
TitleVendorServiceId: 6615159,
ServiceCode: 1OWNER,
CustomVendorInstructions: blah blah blah blah blah
}
}
}
}
}
}
}
}
所以我用了Edit -> Paste special -> Paste XML as Classes
:
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class OrderDetail
{
private uint messageTypeCodeField;
private uint orderDetailIdField;
private OrderDetailClientInfo clientInfoField;
private OrderDetailOrderTaxes orderTaxesField;
private OrderDetailOrderTransactions orderTransactionsField;
/// <remarks/>
public uint MessageTypeCode
{
get
{
return this.messageTypeCodeField;
}
set
{
this.messageTypeCodeField = value;
}
}
/// <remarks/>
public uint OrderDetailId
{
get
{
return this.orderDetailIdField;
}
set
{
this.orderDetailIdField = value;
}
}
/// <remarks/>
public OrderDetailClientInfo ClientInfo
{
get
{
return this.clientInfoField;
}
set
{
this.clientInfoField = value;
}
}
/// <remarks/>
public OrderDetailOrderTaxes OrderTaxes
{
get
{
return this.orderTaxesField;
}
set
{
this.orderTaxesField = value;
}
}
/// <remarks/>
public OrderDetailOrderTransactions OrderTransactions
{
get
{
return this.orderTransactionsField;
}
set
{
this.orderTransactionsField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailClientInfo
{
private string clientNameField;
/// <remarks/>
public string ClientName
{
get
{
return this.clientNameField;
}
set
{
this.clientNameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTaxes
{
private OrderDetailOrderTaxesOrderTax orderTaxField;
/// <remarks/>
public OrderDetailOrderTaxesOrderTax OrderTax
{
get
{
return this.orderTaxField;
}
set
{
this.orderTaxField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTaxesOrderTax
{
private uint taxIdField;
/// <remarks/>
public uint TaxId
{
get
{
return this.taxIdField;
}
set
{
this.taxIdField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactions
{
private OrderDetailOrderTransactionsOrderTransaction orderTransactionField;
/// <remarks/>
public OrderDetailOrderTransactionsOrderTransaction OrderTransaction
{
get
{
return this.orderTransactionField;
}
set
{
this.orderTransactionField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactionsOrderTransaction
{
private object loanAmountField;
private OrderDetailOrderTransactionsOrderTransactionTitle titleField;
/// <remarks/>
public object LoanAmount
{
get
{
return this.loanAmountField;
}
set
{
this.loanAmountField = value;
}
}
/// <remarks/>
public OrderDetailOrderTransactionsOrderTransactionTitle Title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitle
{
private OrderDetailOrderTransactionsOrderTransactionTitleTitleVendors titleVendorsField;
/// <remarks/>
public OrderDetailOrderTransactionsOrderTransactionTitleTitleVendors TitleVendors
{
get
{
return this.titleVendorsField;
}
set
{
this.titleVendorsField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitleTitleVendors
{
private OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor titleVendorField;
/// <remarks/>
public OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor TitleVendor
{
get
{
return this.titleVendorField;
}
set
{
this.titleVendorField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendor
{
private string vendorInstructionsField;
private OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServices vendorServicesField;
/// <remarks/>
public string VendorInstructions
{
get
{
return this.vendorInstructionsField;
}
set
{
this.vendorInstructionsField = value;
}
}
/// <remarks/>
public OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServices VendorServices
{
get
{
return this.vendorServicesField;
}
set
{
this.vendorServicesField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServices
{
private OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService titleVendorServiceField;
/// <remarks/>
public OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService TitleVendorService
{
get
{
return this.titleVendorServiceField;
}
set
{
this.titleVendorServiceField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetailOrderTransactionsOrderTransactionTitleTitleVendorsTitleVendorVendorServicesTitleVendorService
{
private uint titleVendorServiceIdField;
private string serviceCodeField;
private string customVendorInstructionsField;
/// <remarks/>
public uint TitleVendorServiceId
{
get
{
return this.titleVendorServiceIdField;
}
set
{
this.titleVendorServiceIdField = value;
}
}
/// <remarks/>
public string ServiceCode
{
get
{
return this.serviceCodeField;
}
set
{
this.serviceCodeField = value;
}
}
/// <remarks/>
public string CustomVendorInstructions
{
get
{
return this.customVendorInstructionsField;
}
set
{
this.customVendorInstructionsField = value;
}
}
}
所有使用的代码:
var xmlString = @
"<OrderDetail>
<MessageTypeCode>82540</MessageTypeCode>
<OrderDetailId>59339463</OrderDetailId>
<ClientInfo>
<ClientName>LenderName will appear here</ClientName>
</ClientInfo>
<OrderTaxes>
<OrderTax>
<TaxId>9202225</TaxId>
</OrderTax>
</OrderTaxes>
<OrderTransactions>
<OrderTransaction>
<LoanAmount/>
<Title>
<TitleVendors>
<TitleVendor>
<VendorInstructions>blah blah blah blah .</VendorInstructions>
<VendorServices>
<TitleVendorService>
<TitleVendorServiceId>6615159</TitleVendorServiceId>
<ServiceCode>1OWNER</ServiceCode>
<CustomVendorInstructions>blah blah blah blah blah </CustomVendorInstructions>
</TitleVendorService>
</VendorServices>
</TitleVendor>
</TitleVendors>
</Title>
</OrderTransaction>
</OrderTransactions>
</OrderDetail>";
var xml = new OrderDetail();
System.Xml.Serialization.XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(typeof(OrderDetail));
using(XmlReader reader = XmlReader.Create(new StringReader(xmlString))) {
xml = (OrderDetail) serializer.Deserialize(reader);
}
var xmlDump = xml.Dump();
既然这仍然是开放的,我想我会抛出一个快速的答案,适合XmlSerializer
。在创建类时,我假设了一个类似于这样的模式:
<Foo>
<Bars>
<Bar>
表示类"Foo"包含类"Bar"的实例列表。像这样的模式:
<Foo>
<Bar>
表示类"Foo"包含对类"Bar"实例的单个引用。因此,您应该在"OrderTransaction"中只有一个"Title",而不是它们的列表:
public class OrderDetail
{
public string MessageTypeCode { get; set; }
public string OrderDetailId { get; set; }
public ClientInfo ClientInfo { get; set; }
public List<OrderTax> OrderTaxes { get; set; }
public List<OrderTransaction> OrderTransactions { get; set; }
}
public class OrderTransaction
{
public string LoanAmount { get; set; }
public Title Title { get; set; }
}
public class Title
{
public List<TitleVendor> TitleVendors { get; set; }
}
public class TitleVendor
{
public string VendorInstructions { get; set; }
public List<TitleVendorService> VendorServices { get; set; }
}
public class TitleVendorService
{
public string TitleVendorServiceId { get; set; }
public string ServiceCode { get; set; }
public string CustomVendorInstructions { get; set; }
}
public class ClientInfo
{
public string ClientName { get; set; }
}
public class OrderTax
{
public string TaxId { get; set; }
}
实际上不需要任何与xml相关的装饰。XmlElement
和XmlArray
仅在您想要覆盖XmlSerializer的默认选择时才需要,例如,重命名元素或处理具有多态元素的数组。为了清晰,或者为了防止将来重命名属性,您可以添加它们,但为了简单起见,我没有这样做。
这是测试设置。我从字符串中创建一个"OrderDetail"实例,然后将其重新序列化为XML,然后将原始和重写的XML保存为文件,并在命令提示符窗口中区分它们:
public static class TestOrderDetail
{
public static string TestString =
@"<OrderDetail>
<MessageTypeCode>82540</MessageTypeCode>
<OrderDetailId>59339463</OrderDetailId>
<ClientInfo>
<ClientName>LenderName will appear here</ClientName>
</ClientInfo>
<OrderTaxes>
<OrderTax>
<TaxId>9202225</TaxId>
</OrderTax>
</OrderTaxes>
<OrderTransactions>
<OrderTransaction>
<LoanAmount/>
<Title>
<TitleVendors>
<TitleVendor>
<VendorInstructions>blah blah blah blah .</VendorInstructions>
<VendorServices>
<TitleVendorService>
<TitleVendorServiceId>6615159</TitleVendorServiceId>
<ServiceCode>1OWNER</ServiceCode>
<CustomVendorInstructions>blah blah blah blah blah </CustomVendorInstructions>
</TitleVendorService>
</VendorServices>
</TitleVendor>
</TitleVendors>
</Title>
</OrderTransaction>
</OrderTransactions>
</OrderDetail>";
public static void Test()
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(OrderDetail));
OrderDetail orderDetail = (OrderDetail)serializer.Deserialize(new StringReader(TestString));
string newTestString = TestWrite(serializer, orderDetail);
var guid = DateTime.Now.Ticks;
var path = Path.GetTempPath();
var file1 = path + Path.DirectorySeparatorChar + "OldOrderDetail_" + guid.ToString() + ".xml";
var file2 = path + Path.DirectorySeparatorChar + "NewOrderDetail_" + guid.ToString() + ".xml";
File.WriteAllText(file1, TestString);
File.WriteAllText(file2, newTestString);
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
}
private static string TestWrite(XmlSerializer serializer, OrderDetail orderDetail)
{
using (var textWriter = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true; // For testing purposes, disable the xml version and encoding declarations.
settings.Indent = true;
settings.IndentChars = " "; // The indentation used in the test string.
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // For testing purposes, disable the xmlns:xsi and xmlns:xsd lines, which were not in the test string.
serializer.Serialize(xmlWriter, orderDetail, ns);
}
return textWriter.ToString();
}
}
}
下面是区分新旧XML文件的结果:
diff OldOrderDetail_635420738451748332.xml NewOrderDetail_635420738451748332.xml
14c14
< <LoanAmount/>
---
> <LoanAmount />
除了在空的"LoanAmount"元素中插入一个额外的格式化空间外,没有什么区别。
顺便说一句,XmlSerializer和DataContractSerializer是完全不同的。根据您的示例,需要使用XmlSeralizer(而不是DataContractSerializer)。
这是我目前所做的。这段代码产生了以下结构:
-OrderTransactions (Count = 1)
- Loan Amount = ""
- Titles (Count = 1)
-TitleVendors (Count = 1)
- VendorInstructions = null
- VendorServices (Count = 0)
它似乎看到了titlevendor内部的节点,但它没有抓取信息。
<OrderTransactions>
<OrderTransaction>
<LoanAmount /> --this may be NULL - this should not cause the message to fail.
<Title>
<TitleVendors>
<TitleVendor>
<VendorInstructions>Endorsements required: EPA, COMP, PUD. **Attention Abstractor: THIS IS A VA LOAN** **Attention Abstractor: If a PRIVATE ROAD EASEMENT exists, please provide any information and copies along with the abstract.</VendorInstructions>
<VendorServices>
<TitleVendorService>
<TitleVendorServiceId>6615159</TitleVendorServiceId>
<ServiceCode>1OWNER</ServiceCode>
<CustomVendorInstructions><p><b>Copies of recital page, legal description and signature pages of all open mortgages must be provided including copies of the legal description and any riders.<br /> <br /> Copies of assignments must be provided for open liens.<br /> <br /> If the property is registered land a copy of the certificate of title must accompany the search</CustomVendorInstructions>
</TitleVendorService>
</VendorServices>
</TitleVendor>
</TitleVendors>
</Title>
</OrderTransaction>
</OrderTransactions>
[XmlArray("OrderTransactions")]
[XmlArrayItem("OrderTransaction")]
public List<OrderTransaction> OrderTransactions = new List<OrderTransaction>();
public class OrderTransaction
{
[XmlElement("LoanAmount")]
[DataMember]
public string LoanAmount { get; set; }
[XmlArray("Title")]
[XmlArrayItem("TitleVendors")]
public List<Title> Titles = new List<Title>();
public class Title
{
[XmlArray("TitleVendor")]
[XmlArrayItem("VendorInstructions")]
//[XmlArrayItem("VendorServices")]
public List<TitleVendor> TitleVendors = new List<TitleVendor>();
public class TitleVendor
{
[XmlElement("VendorInstructions")]
[DataMember]
public string VendorInstructions { get; set; }
[XmlArray("VendorServices")]
[XmlArrayItem("TitleVendorService")]
public List<VendorService> VendorServices = new List<VendorService>();
public class VendorService
{
public List<TitleVendorService> TitleVendorServices = new List<TitleVendorService>();
public class TitleVendorService
{
[XmlElement("TitleVendorServiceId")]
[DataMember]
public string TitleVendorServiceId { get; set; }
[XmlElement("ServiceCode")]
[DataMember]
public string ServiceCode { get; set; }
[XmlElement("CustomVendorInstructions")]
[DataMember]
public string CustomVendorInstructions { get; set; }
}
}
}
}
}