【问题标题】:How to convert nested list to XML in C#如何在 C# 中将嵌套列表转换为 XML
【发布时间】:2018-06-24 21:56:33
【问题描述】:

我正在使用以下方法将列表转换为 XML。我如何更改它以便将嵌套列表转换为 XML。

private string ConvertProductListToXML(List<ProductDM> productDMList)
{
    var xEle = new XElement("Products",
       from emp in productDMList
       select new XElement("Product",
                    new XElement("ProductID", emp.ProductID),
                      new XElement("Cost", emp.Cost),
                      new XElement("UPC", emp.UPC),
                      new XElement("TaxStatus", emp.TaxStatus)
    ));
    return  ConvertToInnerXML(xEle);
}

public static string ConvertToInnerXML(XElement el)
{
    var reader = el.CreateReader();
    reader.MoveToContent();
    return reader.ReadInnerXml();
}

型号

public class ProductDM
{
    public int? ProductID { get; set; }
    public string UPC { get; set; }
    public string TaxStatus { get; set; }
    public Decimal Cost { get; set; }
}

如果数据模型看起来像下面的扩展列表,我该如何修改上面的代码。

public class ProductDM
{
    public int? ProductID { get; set; }
    public string UPC { get; set; }
    public string TaxStatus { get; set; }
    public Decimal Cost { get; set; }
    public List<InventoryDM> CabinetList { get; set; }
}

public class InventoryDM
{
    public int InventoryID { get; set; }
}

预期输出:

<Product><ProductID>40</ProductID><Cost>2</Cost><UPC>3121</UPC>
<TaxStatus>NO</TaxStatus><CabinetList>
<InventoryID>1</InventoryID></CabinetList><CabinetList>
<InventoryID>2</InventoryID></CabinetList></Product>

【问题讨论】:

  • 您希望新的 XML 看起来像什么?
  • 你看过 XML 序列化吗?我相信您的需求可以使用 System.Xml.Serialization 命名空间中的工具轻松完成。
  • @Richard 问题已修改。请看一下
  • 请在提问前先写一些代码。
  • 我同意你应该序列化。只是为了回答这个问题,请尝试以下操作: new XElement("TaxStatus", emp.TaxStatus), emp.CabinetList.Select(x => new XElement("CabinetList", new XElement("InventoryID", x.InventoryID)) 我不'不知道你为什么需要多个 CabinetList 标签。所有的 InventoryID 都应该在一个 CabinetList 下。

标签: c# xml list xelement xmlconvert


【解决方案1】:

我不会使用 xElement。 最简单的解决方案是使用 xml 序列化。 你的课程应该像这样更新

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace YourAppNameSpace
{
    [XmlRoot(ElementName="CabinetList")]
    public class InventoryDM {
        [XmlElement(ElementName="InventoryID")]
        public string InventoryID { get; set; }
    }

    [XmlRoot(ElementName="Product")]
    public class Product {
        [XmlElement(ElementName="ProductID")]
        public int? ProductID { get; set; }
        [XmlElement(ElementName="Cost")]
        public Decimal Cost { get; set; }
        [XmlElement(ElementName="UPC")]
        public string UPC { get; set; }
        [XmlElement(ElementName="TaxStatus")]
        public string TaxStatus { get; set; }
        [XmlElement(ElementName="CabinetList")]
        public List<InventoryDM> CabinetList { get; set; }
    }
}

并将类序列化为 xml

public static string SerializeObject(object obj)
        {
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                serializer.Serialize(ms, obj);
                ms.Position = 0;
                xmlDoc.Load(ms);
                return xmlDoc.InnerXml;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    相关资源
    最近更新 更多