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 system. I have read about XMLWriter, but thought I would see if there are any other better methods. Any suggestions would be greatly appreciated.
Example XML:
<?xml version="1.0"?> <Orders> <Order OrderNumber="12345"> <ItemNumber>0123993587</ItemNumber> <QTY>10</QTY> <WareHouse>PA019</WareHouse> </Order> <Order OrderNumber="12346"> <ItemNumber>0123993587</ItemNumber> <QTY>9</QTY> <WareHouse>PA019</WareHouse> </Order> <Order OrderNumber="12347"> <ItemNumber>0123993587</ItemNumber> <QTY>8</QTY> <WareHouse>PA019</WareHouse> </Order> </Orders>
The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. DOM stands for document object model. To read more about it, see XML Document Object Model (DOM).
From the menu bar, choose Tools > Options to open the Options dialog box. Then, navigate to Text Editor > C# (or Visual Basic) > Advanced. In the Editor Help section, look for the Generate XML documentation comments option.
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.
I would suggest using the classes in System.Xml.Linq.dll which contain an XML DOM API that allows for easy build-up of XML structures due to the way the contructors are designed. Trying to create an XML structure using the System.Xml classes is very painful because you have to create them detached then separately add them into the document.
Here's an example of XLinq vs. System.Xml to create a DOM from scratch. Your eyes will bleed when you see the System.Xml example.
Here's a quick example of how you would use XLinq to build up part of your doc.
var xml = new XElement("Orders", new XElement("Order", new XAttribute("OrderNumber", 12345), new XElement("ItemNumber", "01234567"), new XElement("QTY", 10), new XElement("Warehouse", "PA019") ) );
TIP Although it's a little unorthodox (though no worse than some of the language butchering that has become popular lately), I have on occasion used C#'s type aliasing feature to minimize the code even further:
using XE = System.Xml.Linq.XElement; using XA = System.Xml.Linq.XAttribute; ... var xml = new XE("Orders", new XE("Order", new XA("OrderNumber", 12345), new XA("ItemNumber", "01234567"), new XA("QTY", 10), new XA("Warehouse", "PA019") ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With