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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…