jjzjj

c# - 复杂的 XML 反序列化

coder 2024-06-29 原文

我正在尝试反序列化一个复杂的 XML 文件。我有我的主类结构,因此它获取第一个子节点中的所有信息,我什至拥有它以便我可以获得两层深的 ClientName。但是,除此之外的任何事情似乎都不起作用。我得到一个计数为 1 的列表项,但列表中没有任何信息。

我的 OrderTaxesOrderTransactions 列表返回一个 Count = 1 但所有字段都是空的。

我肯定这是我的类(class)结构的问题,非常感谢任何帮助纠正这个问题。

这是 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 运行了一个示例,以证明它可以正常工作。

  1. 首先,我将您的 XML 保存到我桌面上名为“sample.xml”的文件中
  2. 我以管理员身份打开了 VS 2012 命令提示符。
  3. 在命令提示符中,我导航到安装 SDK 的目录。对我来说,它安装在这里(可能因不同的操作系统或 VS 版本而不同):

    C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 工具

  4. 我运行了以下命令来为您的 XML 创建 XSD。 (期望sample.xsd出现在桌面上)

    xsd "c:\users\glenn\desktop\sample.xml"/outputdir:"c:\users\glenn\desktop"

  5. 我运行了以下命令,从生成的 XSD 文件创建了一个 CSharp 文件。

    xsd "c:\users\glenn\desktop\sample.xsd"/classes/outputdir:"c:\users\glenn\desktop"

不包括注释,这是为您的 XML 模式自定义生成的 CSharp 文件。还有其他选项可以设置 namespace 和类型名称。请查看上面的文章以了解该详细信息。

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

希望对您有所帮助!

关于c# - 复杂的 XML 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24921577/

有关c# - 复杂的 XML 反序列化的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  3. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  4. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  5. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  6. ruby - 使用 AES 的 Rails 加密,过于复杂 - 2

    我在加密来self正在使用的第三方供应商的值时遇到问题。他们的指令如下:1)Converttheencryptionpasswordtoabytearray.2)Convertthevaluetobeencryptedtoabytearray.3)Theentirelengthofthearrayisinsertedasthefirstfourbytesontothefrontofthefirstblockoftheresultantbytearraybeforeencryption.4)EncryptthevalueusingAESwith:1.256-bitkeysize,2.25

  7. ruby - 测试一个复杂的方法 - 2

    我正在开发西洋跳棋实现,其中有许多易于测试的方法,但我不确定如何测试我的主要#play_game方法。我的大多数方法都可以很容易地确定输入和输出,因此也很容易测试,但这种方法是多方面的,实际上并没有容易辨别的输出。这是代码:defplay_gameputs@gui.introwhile(game_over?==false)message=nil@gui.render_board(@board)@gui.move_requestplayer_input=getscoordinates=UserInput.translate_move_request_to_coordinates(play

  8. ruby - 是否有用于复杂比较的漂亮语法? - 2

    方法应返回-1,0或1分别表示“小于”、“等于”和“大于”。对于某些类型的可排序对象,通常将排序顺序基于多个属性。以下是可行的,但我认为它看起来很笨拙:classLeagueStatsattr_accessor:points,:goal_diffdefinitializepts,gd@points=pts@goal_diff=gdenddefothercompare_pts=pointsother.pointsreturncompare_ptsunlesscompare_pts==0goal_diffother.goal_diffendend尝试一下:[LeagueStats.new(

  9. ruby-on-rails - carrierwave:在序列化动态属性上安装 uploader - 2

    首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟

  10. ruby-on-rails - 如何构建复杂的 Rails 系统 - 2

    关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭8年前。Improvethisquestion我们有以下(以及更多)系统,我们将数据从一个应用推送/拉取到另一个:托管CRM(InsideSales.com)Asterisk电话系统(内部)横幅广告系统(openx,我们托管)潜在客户生成系统(自行开发)电子商务商店(spree,我们托管)工作板(本土)一些工作网站抓取+入站工作提要电子邮件传送系统(如Mailchimp,自主开发)事件管理系统(如eventbrite,自主开发)仪表板系统(大量图表和

随机推荐