Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best .net Method to create an XML Doc

Tags:

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> 
like image 704
scarpacci Avatar asked Jan 16 '10 06:01

scarpacci


People also ask

Which class is used to create an XML document is?

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).

How do I create an XML document in Visual Studio?

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.


2 Answers

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.

like image 111
Jon Skeet Avatar answered Sep 25 '22 13:09

Jon Skeet


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")     ) ); 
like image 45
Josh Avatar answered Sep 21 '22 13:09

Josh