1

I am using the below method to convert a list to XML. How can I alter this in order to convert a nested list in to 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();
}

Model

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

How can I modify the above code if the data model looks like below with an extended list.

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

Expected output :

<Product><ProductID>40</ProductID><Cost>2</Cost><UPC>3121</UPC>
<TaxStatus>NO</TaxStatus><CabinetList>
<InventoryID>1</InventoryID></CabinetList><CabinetList>
<InventoryID>2</InventoryID></CabinetList></Product>
5
  • What do you want the new XML to look like? Commented Jan 16, 2018 at 8:39
  • Have you had a look at XML serialization? I believe your requirements can be easily done with the tools from the System.Xml.Serialization namespace. Commented Jan 16, 2018 at 8:42
  • @Richard Question modified. Please have a look Commented Jan 16, 2018 at 8:45
  • Please try writing some code first before asking questions. Commented Jan 16, 2018 at 9:05
  • I agree that you should serialize. Just to answer the question try following : new XElement("TaxStatus", emp.TaxStatus), emp.CabinetList.Select(x => new XElement("CabinetList", new XElement("InventoryID", x.InventoryID)) I don't know why you need multiple CabinetList tags. All the InventoryID should be under one CabinetList. Commented Jan 16, 2018 at 10:52

1 Answer 1

2

I wouldn't use xElement. The easiest solution is to use xml serialization. Your class should be updated like this

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

And to serialze the class to 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;
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.