Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to construct an XML document from a string containing well formed XML for navigation?

Tags:

c#

.net

xml

I have a string that contains well formed xml in it. I want to navigate the XML in that string to extract the text in certain nodes. How do I efficiently accomplish this using a built-in .NET class. Which .NET XML class would you use and why?

Many thanks for your help.

Note 1: Linq is not available to me. Note 2: Editing the XML is not important. Read-only access is what I need.

like image 650
Newbie Avatar asked Sep 28 '09 21:09

Newbie


People also ask

What is XML What are the requirements for an XML document to be Well Formed?

An XML document is called well-formed if it satisfies certain rules, specified by the W3C. These rules are: A well-formed XML document must have a corresponding end tag for all of its start tags. Nesting of elements within each other in an XML document must be proper.

What are Well Formed and valid documents in XML?

An XML document with correct syntax is called "Well Formed". An XML document validated against a DTD is both "Well Formed" and "Valid".

What is well formed XML document in Web technology?

A well-formed document in XML is a document that "adheres to the syntax rules specified by the XML 1.0 specification in that it must satisfy both physical and logical structures".


2 Answers

For speed, use an XmlReader:

using (StringReader sr = new StringReader(myString))
using (XmlReader xr = XmlReader.Create(sr))
{
    while (xr.Read())
    {
        if (xr.NodeType == XmlNodeType.Element && xr.Name == "foo")
        {
            Console.WriteLine(xr.ReadString());
        }
    }
}

The above prints out the text content of every element named "foo" in the XML document. (Well, sort of. ReadString doesn't handle nested elements very gracefully.)

Using an XPathDocument is slower, because the entire document gets parsed before you can start searching it, but it has the merit of simplicity:

using (StringReader sr = new StringReader(myString))
{
    XPathDocument d = new XPathDocument(sr);
    foreach (XPathNavigator n in d.CreateNavigator().Select("//foo/text()"))
    {
        Console.WriteLine(n.Value);
    }
}

If you're not concerned with performance or memory utilization, it's simplest to use an XmlDocument:

XmlDocument d = new XmlDocument();
d.LoadXml(myString);
foreach (XmlNode n in d.SelectNodes("//foo/text()"))
{
   Console.WriteLine(n.Value);
}
like image 119
Robert Rossney Avatar answered Oct 05 '22 02:10

Robert Rossney


For navigation? Probably XPathDocument:

string s = @"<xml/>";
XPathDocument doc = new XPathDocument(new StringReader(s));

From MSDN,

Provides a fast, read-only, in-memory representation of an XML document by using the XPath data model.

Unlike XmlDocument etc, it is optimised for readonly usage; more efficient but less powerful (i.e. you can't edit it). For notes on how to query it, see here.

like image 38
Marc Gravell Avatar answered Oct 05 '22 04:10

Marc Gravell