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
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<Order> 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);
}
I found it largely depends how complex your original data is.
If your data is nicely organized in objects and it is sufficient to dump it in XML, Linq is very verbose and powerful. But as soon as there are object inter-dependencies, I don't think you want to go with the Linq one-liner, as this is real pain to debug and/or extend.
For those cases, I'd prefer to go with XmlDocument, create a help method to facilitate adding attributes to an element (see below), and use Linq in foreach loops around the XML creation blocks.
private void XAttr(ref XmlNode xn, string nodeName, string nodeValue)
{
XmlAttribute result = xn.OwnerDocument.CreateAttribute(nodeName);
result.InnerText = nodeValue;
xn.Attributes.Append(result);
}
You just right click on your code window , select InsertSnippet follow this Link
Data-Xml.....>Xml>Xmlcreate
its very easy
Josh's answer shows how easy it is to create a single element in LINQ to XML... it doesn't show how it's also hugely easy to create multiple elements. Suppose you have a List<Order>
called orders
... you can create the whole document like this:
var xml = new XElement("Orders",
orders.Select(order =>
new XElement("Order",
new XAttribute("OrderNumber", order.OrderNumber),
new XElement("ItemNumber", order.ItemNumber),
new XElement("QTY", order.Quantity),
new XElement("Warehouse", order.Warehouse)
)
)
);
LINQ to XML makes constructing XML incredibly easy. It also has support for XML namespaces which is pretty easy too. For instance, if you wanted your elements to be in a particular namespace, you'd just need:
XNamespace ns = "http://your/namespace/here";
var xml = new XElement(ns + "Orders",
orders.Select(order =>
new XElement(ns + "Order",
... (rest of code as before)
LINQ to XML is the best XML API I've worked with... it's great for querying too.
There's a new language called XCST that compiles to C#.
<c:template name='c:initial-template' expand-text='yes'>
<c:param name='orders' as='IEnumerable<Order>'/>
<Orders>
<c:for-each name='order' in='orders'>
<Order OrderNumber='{order.Number}'>
<ItemNumber>{order.ItemNumber}</ItemNumber>
<QTY>{order.Quantity}</QTY>
<WareHouse>{order.WareHouse}</WareHouse>
</Order>
</c:for-each>
</Orders>
</c:template>
What about this : create a class "Order" and one "Orders", and then serialize those out to XML - seems a lot easier to me than creating the XML bit by bit from hand....
Since you say you're pulling the data off your ERP, you probably already have objects and classes for "Order" and so on - maybe it's sufficient to put a few [XmlElement] attributes on your classes and you're good to go!
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace XmlLinqTest
{
[Serializable]
[XmlRoot(Namespace = "")]
public class Orders
{
private List<Order> _orders = new List<Order>();
/// <remarks/>
[XmlElement("Order")]
public List<Order> OrderList
{
get { return _orders; }
}
}
/// <remarks/>
[Serializable]
public class Order
{
/// <remarks/>
[XmlElement]
public string ItemNumber { get; set; }
[XmlElement]
public int QTY { get; set; }
/// <remarks/>
[XmlElement]
public string WareHouse { get; set; }
/// <remarks/>
[XmlAttribute]
public string OrderNumber { get; set; }
}
}
and in your main app something like this:
Orders orders = new Orders();
Order work = new Order() { ItemNumber = "0123993587", OrderNumber = "12345", QTY = 10, WareHouse = "PA019" };
orders.OrderList.Add(work);
work = new Order() { ItemNumber = "0123993587", OrderNumber = "12346", QTY = 9, WareHouse = "PA019" };
orders.OrderList.Add(work);
work = new Order() { ItemNumber = "0123993587", OrderNumber = "12347", QTY = 8, WareHouse = "PA019" };
orders.OrderList.Add(work);
XmlSerializer ser = new XmlSerializer(typeof(Orders));
using(StreamWriter wr = new StreamWriter(@"D:\testoutput.xml", false, Encoding.UTF8))
{
ser.Serialize(wr, orders);
}
Working with objects and then serializing them out to disk seems a lot easier to me than fiddling around with XDocument and other APIs.