views:

99

answers:

7

I have a StringBuilder with the contents of an XML file. Inside the XML file is a root tag called <root> and contains multiple <node> tags.

I'd like to parse through the XML to read values of tags within in s, but not sure how to do it.

Will I have to use some C# XML data type for this?

Thanks in advance

A: 

use XDocument.Parse(...)

Noel Abrahams
A: 

Yes, I suggest you use an XmlDocument object to parse the content of your string. Here is an example who print all inner text contained in your tags:

var doc=new XmlDocument();
doc.LoadXml(stringBuilder.TosTring());
XmlNodeList elemList = doc.GetElementsByTagName("node");
for (int i=0; i < elemList.Count; i++)
{   
  XmlNode node=elemList[i];
  Console.WriteLine(node.InnerText);
}  

using Node object members, you can also easily extract all you attributes .

Andrea Parodi
using LoadXml is very memory in-efficient. It's memory footprint is too high (almost 10x times to normal xml footprint). The user just want to read the data so we can do it without loading to memory.
A_Var
+1  A: 

You should use classes available in either System.Xml or System.Xml.Linq to parse XML.

XDocument is part of the LINQ extensions for XML and is particularly easy to use if you need to parse through an arbitrary structure. I would suggest using it rather than XmlDocument (unless you have legacy code or are not on .NET 3.5).

Creating an XDocument from a StringBuilder is straightforward:

var doc = XDocument.Parse( stringBuilder.ToString() );

From here, you can use FirstNode, Descendents(), and the many other properties and methods available to walk and examine the XML structure. And since XDocument is designed to work well with LINQ, you can also write queries like:

var someData = from node in doc.Descendants ("yourNodeType")
               select node.Value; // etc..
LBushkin
A: 

There are several objects at your disposal for working with XML. Look at the System.Xml namespace for objects such as XmlDocument as well as the XmlReader and XmlWriter families of objects. If using C# 3.0+, look at the System.Xml.Linq namespace and the XDocument class.

Anthony Pegram
+2  A: 
StringBuilder sb = new StringBuilder (xml);

TextReader textReader = new StringReader (sb.ToString ());
XDocument xmlDocument = XDocument.Load (textReader);

var nodeValueList = from node in xmlDocument.Descendants ("node")
                    select node.Value;
Developer Art
A: 

If you're looking to read all the values in the XML file , you could look into deserializing the XML into a C# data Object.

Deserializing XML into class obj in C#

xbonez
+1  A: 

If you are just looking the specifically named nodes then you don't need to load the document into memory, you can process it yourself with an XmlReader.

using(var sr = new StringReader(stringBuilder.ToString)) {
  using(var xr = XmlReader.Create(sr)) {
    while(xr.Read()) {
      if(xr.IsStartElement() && xr.LocalName == "node")
        xr.ReadElementString(); //Do something here
    }
  }
}
Nick Jones
This is way more efficient than above methods. Almost zero memory footprint. Awesome!!.
A_Var