在我的电子商务中,我需要创建用户订单的XML 我有两个实体Order和OrderItems 这些表与外键关联 在httpget中,我传递订单ID,然后从此处创建xml 是我第一次尝试创建这样的xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<doc>
<table name="order">
<row>
<field name="Id">1191549131629607</field>
<field name="Year">2019</field>
...
</row>
</table>
<table name="items">
<row>
<field name="Id">1191549131629607</field>
<field name="ProductId">A01</field>
<field name="Quantity">1</field>
...
</row>
<row>
<field name="Id">1191549131629607</field>
<field name="ProductId">A02</field>
<field name="Quantity">1</field>
...
</row>
</table>
</doc>
现在我只获得表头,但不知道如何插入orderItems
[HttpGet("{id}")]
public async Task<IActionResult> CreateXml(Guid id)
{
var orders = await _ordersRepository.GetOrders();
var orderItems = await _orderItemsRepository.GetOrderItems();
var ordersXml =
new XElement("doc",
new XElement("table", new XAttribute("name", "orders"),
from x in orders
where x.RolCodEst == id
select new XElement("row",
new XElement("field", new XAttribute("name", "Id"), x.Id),
new XElement("field", new XAttribute("name", "Year"), x.Year)
)
)
);
XDocument xdoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", null), ordersXml);
string dt = DateTime.Now.ToString("yyyyMMddhhmmss");
var path = Path.Combine(_hosting.ContentRootPath, $"Xml");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
string filePath = Path.Combine(_hosting.ContentRootPath, $"Xml/0_dati_{dt}.xml");
xdoc.Save(filePath);
return Ok(ordersXml);
}