Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

How to convert class Array to XML file in C# dynamically?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...