How to work with XML in C #

What is the best way to work with XML documents, XSD, etc. in c # 2.0?

What classes to use, etc. What are the best methods for parsing and creating XML documents, etc.

EDIT: .Net 3.5 is also welcome.

+70
c # xml
Oct 21 '08 at 5:35
source share
11 answers

The main readers and writers in C # 2.0 are executed using the XmlDocument class. You can load most of your settings directly into the XmlDocument through the XmlReader that it accepts.

XML loading directly

XmlDocument document = new XmlDocument(); document.LoadXml("<People><Person Name='Nick' /><Person Name='Joe' /></People>"); 

Loading XML from a file

 XmlDocument document = new XmlDocument(); document.Load(@"C:\Path\To\xmldoc.xml"); // Or using an XmlReader/XmlTextReader XmlReader reader = XmlReader.Create(@"C:\Path\To\xmldoc.xml"); document.Load(reader); 

I find the easiest / fastest way to read an XML document using XPath.

Reading an XML document using XPath (using an XmlDocument that allows us to edit)

 XmlDocument document = new XmlDocument(); document.LoadXml("<People><Person Name='Nick' /><Person Name='Joe' /></People>"); // Select a single node XmlNode node = document.SelectSingleNode("/People/Person[@Name = 'Nick']"); // Select a list of nodes XmlNodeList nodes = document.SelectNodes("/People/Person"); 

If you need to work with XSD documents to validate an XML document, you can use this.

Validating XML documents against XSD schemas

 XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidateType = ValidationType.Schema; settings.Schemas.Add("", pathToXsd); // targetNamespace, pathToXsd XmlReader reader = XmlReader.Create(pathToXml, settings); XmlDocument document = new XmlDocument(); try { document.Load(reader); } catch (XmlSchemaValidationException ex) { Trace.WriteLine(ex.Message); } 

XML Validation on XSD on Each Node (UPDATE 1)

 XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidateType = ValidationType.Schema; settings.Schemas.Add("", pathToXsd); // targetNamespace, pathToXsd settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); XmlReader reader = XmlReader.Create(pathToXml, settings); while (reader.Read()) { } private void settings_ValidationEventHandler(object sender, ValidationEventArgs args) { // e.Message, e.Severity (warning, error), e.Error // or you can access the reader if you have access to it // reader.LineNumber, reader.LinePosition.. etc } 

Writing an XML Document (Manual)

 XmlWriter writer = XmlWriter.Create(pathToOutput); writer.WriteStartDocument(); writer.WriteStartElement("People"); writer.WriteStartElement("Person"); writer.WriteAttributeString("Name", "Nick"); writer.WriteEndElement(); writer.WriteStartElement("Person"); writer.WriteStartAttribute("Name"); writer.WriteValue("Nick"); writer.WriteEndAttribute(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); 

(UPDATE 1)

In .NET 3.5, you use XDocument to perform similar tasks. The difference, however, is that you have the advantage of executing Linq queries to select the exact data you need. With the addition of object initializers, you can create a query that even returns objects of your own definition directly in the query itself.

  XDocument doc = XDocument.Load(pathToXml); List<Person> people = (from xnode in doc.Element("People").Elements("Person") select new Person { Name = xnode.Attribute("Name").Value }).ToList(); 

(UPDATE 2)

A good way in .NET 3.5 is to use an XDocument to create the XML below. This leads to the fact that the code looks the same way with the desired output.

 XDocument doc = new XDocument( new XDeclaration("1.0", Encoding.UTF8.HeaderName, String.Empty), new XComment("Xml Document"), new XElement("catalog", new XElement("book", new XAttribute("id", "bk001"), new XElement("title", "Book Title") ) ) ); 

creates

 <!--Xml Document--> <catalog> <book id="bk001"> <title>Book Title</title> </book> </catalog> 

Everything else fails, you can check out this MSDN article, which has a lot of examples that I discussed here and more. http://msdn.microsoft.com/en-us/library/aa468556.aspx

+155
Oct 21 '08 at 6:40
source share

It depends on the size; for small and medium sizes xml, DOM, such as XmlDocument (any version of C # /. NET) or XDocument (.NET 3.5 / C # 3.0) is the obvious winner. To use xsd, you can load xml using XmlReader , and XmlReader accepts (to Create ) a XmlReaderSettings . XmlReaderSettings objects have a Schemas property that can be used to validate xsd (or dtd).

The same things apply to the writing of xml, noting that it is slightly easier to host LINQ-to-XML (XDocument) content than the old XmlDocument.

However, for huge xml, the DOM may allocate too much memory, in which case you may need to use XmlReader / XmlWriter directly.

Finally, you can use XslCompiledTransform (xslt layer) to control xml.

An alternative to working with xml is working with an object model; you can use xsd.exe to create classes that represent the xsd-compatible model, and just load the xml as objects, manage it with OO and then serialize those objects again; You do this with the XmlSerializer .

+28
Oct 21 '08 at 5:43
source share

Nyxtom answer is very good. I would add a few things:

If you need read-only access to an XML document, XPathDocument is a much lighter object than XmlDocument .

The downside of using XPathDocument is that you cannot use the familiar SelectNodes and SelectSingleNode XmlNode . Instead, you should use the tools that IXPathNavigable provides: use CreateNavigator to create XPathNavigator and use XPathNavigator to create XPathNodeIterator to iterate over the lists of nodes that you find through XPath. This usually requires a few more lines of code than the XmlDocument methods.

But: the XmlDocument and XmlNode implement IXPathNavigable , so any code you write to use these methods in XPathDocument will also work on XmlDocument . If you are used to writing against IXPathNavigable , your methods can work against any object. (This is why the use of XmlNode and XmlDocument in method signatures is labeled FxCop.)

Unfortunately, XDocument and XElement (and XNode and XObject ) do not implement IXPathNavigable .

Another thing missing from the nyxtom answer is XmlReader . Typically, you use XmlReader to avoid the overhead of parsing the XML stream in the object model before you begin to process it. Instead, you use an XmlReader to process the input stream one XML node at a time. This is essentially a .NET response to SAX. This allows you to write very fast code to process very large XML documents.

XmlReader also provides the easiest way to process fragments of an XML document, for example. an XML element stream without an element that returns the SQL Server FOR XML RAW parameter.

The code you write using XmlReader is usually very closely related to the format of the read XML. Using XPath allows your code to be much, much weaker associated with XML, so it is usually the correct answer. But when you need to use XmlReader , you really need it.

+12
Oct 21 '08 at 18:51
source share

101 Linq samples

http://msdn.microsoft.com/en-us/library/bb387098.aspx

and Linq Examples for XML

http://msdn.microsoft.com/en-us/vbasic/bb688087.aspx

And I think Linq simplifies XML.

+4
Oct 21 '08 at 6:51
source share

First of all, check out the new XDocument and XElement classes as they are an improvement over the previous XmlDocument family.

  • They work with LINQ
  • They are faster and easier.

However , you may have to use old classes to work with legacy code - especially with pre-created proxies. In this case, you will need to familiarize yourself with some templates for the interaction between these XML processing classes.

I think your question is quite broad and will require too much in one answer to give details, but this is the first general answer that I thought is the beginning.

+3
Oct 21 '08 at 5:47
source share

If you work in .NET 3.5 and you are not afraid of experimental code, you can check LINQ on XSD ( http://blogs.msdn.com/xmlteam/archive/2008/02/21/linq-to-xsd-alpha- 0-2.aspx ), which will generate .NET classes from XSD (including built-in rules from XSD).

Then it has the ability to write directly to a file and read from a file, ensuring that it complies with XSD rules.

I definitely suggest having an XSD for any XML document you are working with:

  • Allows you to execute rules in XML
  • Allows others to see how XML will / will be structured.
  • Can be used to validate XML.

I find that Liquid XML Studio is a great tool for creating XSD, and it's free!

+2
Oct 21 '08 at 6:49
source share

If you create a typed dataset in the designer, you automatically get an xsd, a strongly typed object, and you can load and save xml with one line of code.

+1
Oct 21 '08 at 16:41
source share

My personal opinion, as a C # programmer, is that the best way to handle XML in C # is to delegate this part of the code to the VB.NET project. In .NET 3.5, VB.NET has XML literals that make working with XML more intuitive. See here for example:

LINQ to XML Overview in Visual Basic

(Be sure to set the page to display the VB code, not the C # code.)

I would write the rest of the project in C #, but processed the XML in the VB project with a link.

+1
Mar 31 '10 at 18:57
source share

Writing XML Using the XmlDocument Class

 //itemValues is collection of items in Key value pair format //fileName i name of XML file which to creatd or modified with content private void WriteInXMLFile(System.Collections.Generic.Dictionary<string, object> itemValues, string fileName) { string filePath = "C:\\\\tempXML\\" + fileName + ".xml"; try { if (System.IO.File.Exists(filePath)) { XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlNode rootNode = doc.SelectSingleNode("Documents"); XmlNode pageNode = doc.CreateElement("Document"); rootNode.AppendChild(pageNode); foreach (string key in itemValues.Keys) { XmlNode attrNode = doc.CreateElement(key); attrNode.InnerText = Convert.ToString(itemValues[key]); pageNode.AppendChild(attrNode); //doc.DocumentElement.AppendChild(attrNode); } doc.DocumentElement.AppendChild(pageNode); doc.Save(filePath); } else { XmlDocument doc = new XmlDocument(); using(System.IO.FileStream fs = System.IO.File.Create(filePath)) { //Do nothing } XmlNode rootNode = doc.CreateElement("Documents"); doc.AppendChild(rootNode); doc.Save(filePath); doc.Load(filePath); XmlNode pageNode = doc.CreateElement("Document"); rootNode.AppendChild(pageNode); foreach (string key in itemValues.Keys) { XmlNode attrNode = doc.CreateElement(key); attrNode.InnerText = Convert.ToString(itemValues[key]); pageNode.AppendChild(attrNode); //doc.DocumentElement.AppendChild(attrNode); } doc.DocumentElement.AppendChild(pageNode); doc.Save(filePath); } } catch (Exception ex) { } } OutPut look like below <Dcouments> <Document> <DocID>01<DocID> <PageName>121<PageName> <Author>Mr. ABC<Author> <Dcoument> <Document> <DocID>02<DocID> <PageName>122<PageName> <Author>Mr. PQR<Author> <Dcoument> </Dcouments> 
+1
Feb 09 '16 at 10:18
source share

The answer to cookies is good ... but here are detailed instructions on how to create a strongly typed object from XSD (or XML) and serialize / deserialize in a few lines of code:

Instructions

0
Oct 21 '08 at 18:55
source share

nyxtom,

Is there a match for "doc" and "xdoc" in example 1?

 XDocument **doc** = XDocument.Load(pathToXml); List<Person> people = (from xnode in **xdoc**.Element("People").Elements("Person") select new Person { Name = xnode.Attribute("Name").Value }).ToList(); 
0
Jan 29 '11 at
source share



All Articles