-3

I trying to setting up a Xml file whose data are in class array format. The Basic class format is

OrderTemplate Class

public partial class OrderTemplate {
        public int Id { get; set; }
        public int? OrderNumberCounterId { get; set; }
        public int? SerialCounterId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int? Critical { get; set; }

        public Counter OrderNumberCounter { get; set; }
        public ICollection<OperationTemplate> OperationTemplate { get; set; }
        public ICollection<OrderTemplateAssemblyUnit> OrderTemplateAssemblyUnit { get; set; }


}

The Respective conversion function is given below

public async Task<IActionResult> UploadXML([FromBody] OrderTemplate[] XMLData)
        {
            try
            {


                string fileName = "OrderTemplateXMLData.xml";
                var folderName = Path.Combine("Uploads", "XMLUploads");
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                var fullPath = Path.Combine(pathToSave, fileName);
                var dbPath = Path.Combine(folderName, fileName);                               

                 XDocument doc = new XDocument();
// Thinks the problem is here 
                 doc.Add(new XElement("OrderTemplate",XMLData.Select(x => new XElement("item", x)))); 

                if (Convert.ToString(XMLData).Length > 0)
                {

                    return Ok(new { dbPath });
                }
                else
                {
                    return BadRequest();
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error");
            }
        }
    }

CURRENT RESULT

Currently I generated the Xml file with data like this.

<?xml version="1.0" encoding="UTF-8"?>

<OrderTemplate>
  <Data>KappDmsApi.Models.OrderTemplate</Data>
  <Data>KappDmsApi.Models.OrderTemplate</Data>
</OrderTemplate>

ACTUAL RESULT

But I need to get the Data like this with the Header changed.

<OrderTemplate>
    <Order>
        <id>3</id>
        <orderNumberCounterId>1</orderNumberCounterId>
        <serialCounterId>3</serialCounterId>
        <name>Toyota FS Back</name>
        <description>Toyota FS Back</description>
        <critical>0</critical>
        <orderNumberCounter>null</orderNumberCounter>
    </Order>
    <Order>
        <id>6</id>
        <orderNumberCounterId>1</orderNumberCounterId>
        <serialCounterId>3</serialCounterId>
        <name>Toyota FS Cushion</name>
        <description>Toyota FS Cushion</description>
        <critical>0</critical>
        <orderNumberCounter>null</orderNumberCounter>
    </Order>                   
</OrderTemplate>

How to handle this situation? Is there anything to change in the above code?

8
  • Why would you want nest your OrderTemplate elements inside of another OrderTemplate? Makes no sense to me. Commented Aug 29, 2019 at 10:52
  • I have mentioned the near the Actual Result Heading 'get the Data like this with the Header changed.' I will Re edit it.. Commented Aug 29, 2019 at 10:58
  • 2
    There are plenty of examples for XML serialization. Check learn.microsoft.com/de-de/dotnet/api/… Commented Aug 29, 2019 at 11:00
  • 1
    Possible duplicate of Using XmlSerializer with an array in the root element Commented Aug 29, 2019 at 11:00
  • 2
    Use a serializer instead of manually creating/manipulating a XDocument.... Commented Aug 29, 2019 at 11:09

1 Answer 1

2

Instead of manually constructing the XML file, use a XML serializer instance.

For it to correctly generate the structure, use a wrapper-class with decorated properties as follows:

class XmlOrderTemplate {
  [XmlArray("OrderTemplate")]
  [XmlArrayItem("Order")]
  public List<OrderTemplate> Orders {get;set;}
}

using(var sw = new StreamWriter(fullPath)){
  var serializer = new XmlSerializer(typeof(XmlOrderTemplate));
   serializer.Serialize(sw, new XmlOrderTemplate {Orders = Data});
}
Sign up to request clarification or add additional context in comments.

2 Comments

Vielen Dank @ Jota.Toledo , but still this has some error like : 'Cannot serialize member of collection... because it is a interface'. I changed the above code by adding [XmlIgnore] above the Inumerable<OrderTemplate> . After that my output is like <?xml version="1.0" encoding="UTF-8"?> <XmlOrderTemplate xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance">
Vielen Dank @Jota.Toledo !!! . I got i by creating another class by removing public Counter OrderNumberCounter { get; set; } , donot know if its correct, is there any method to handle this . I will Reedit the Class in Question again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.