Best .net Method to create an XML Doc

前端 未结 11 1116
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 03:33

I am trying to figure out what the best method is for writing an XML Document. Below is a simple example of what I am trying to create off of data I am pulling from our ERP syst

11条回答
  •  忘掉有多难
    2021-02-04 03:45

    If you don't want (or can't) to use LINQ to XML, neither to duplicate your Order class to include XML serialization, and thinks XmlWriter is too much verbose, you can go with plain classical XmlDocument class:

    // consider Order class that data structure you receive from your ERP system
    List orders = YourERP.GetOrders();
    XmlDocument xml = new XmlDocument();
    xml.AppendChild(xml.CreateElement("Orders"));
    foreach (Order order in orders)
    {
        XmlElement item = xml.CreateElement("Order");
        item.SetAttribute("OrderNumber", order.OrderNumber);
        item.AppendChild(xml.CreateElement("ItemNumber")).Value = order.ItemNumber;
        item.AppendChild(xml.CreateElement("QTY"       )).Value = order.Quantity;
        item.AppendChild(xml.CreateElement("WareHouse" )).Value = order.WareHouse;
        xml.DocumentElement.AppendChild(item);
    }
    

提交回复
热议问题