Parsing XML from a String

  • Thread starter Thread starter Lindsay
  • Start date Start date
L

Lindsay

I'm new to C# from a C++ background.

A problem I'm having is getting my head around the different XML classes
available.

In particular I have a String type containing XML and want to parse it to
extract values. The XML classes I've seen so far will do it from a file or a
stream but (unless my eyes have failed) I can't find one to do it directly
from a String.

Any help appreciated!

Lindsay
 
Use the Load() method from XMLDocument object.
LoadXml() method is used for loading xml from .xml file.

Regards,
Amal
 
Hello Lindsay,

If you already have the XML in a string format, then you can use the
InnerText property of the XmlDocument to create a DOM Tree. In the example
below, I have a string which contains the xml. I contruct a DOM Tree and
parse its attributes.

using System;
using System.IO;
using System.Xml;

public class XmlFromString
{
public static void Main(string[] args)
{
string xml = "<?xml version='1.0'?><person firstname='john'
lastname='smith' />";
XmlDocument doc = new XmlDocument();
doc.InnerXml = xml;
XmlElement root = doc.DocumentElement;
Console.WriteLine(" The firstname : {0} lastname: {1}",
root.GetAttribute("firstname"), root.GetAttribute("lastname"));
}
}

regards,
Abhishek.
 
I would also suggest the XmlTextReader as another option. This provides fast, forward only reading of xml. To parse an Xml string, use the following:

XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString));

Cheers
Dan
 
string result;
DataSet ds = new DataSet();
TextWriter tw = new StreamWriter("XMLString");
tw.WriteLine(result.ToString()); //result is your string input
tw.Close();
ds.ReadXml("XMLString");

foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["put the element here"]); ......
}

Hope this helps!
 
GeRmIc said:
string result;
DataSet ds = new DataSet();
TextWriter tw = new StreamWriter("XMLString");
tw.WriteLine(result.ToString()); //result is your string input
tw.Close();
ds.ReadXml("XMLString");

foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["put the element here"]); ......
}

Hope this helps!

Don't get me wrong, but this is incredibly awkward. There are numerous ways
to parse XML from a string without performing rain dances like that one.

Cheers,
 
Hi Jooss,

I know, but does'nt this simple code gets the job done? You could perhaps
post some other ways to do it too so that the next person could benefit from
it.

Cheers!

Joerg Jooss said:
GeRmIc said:
string result;
DataSet ds = new DataSet();
TextWriter tw = new StreamWriter("XMLString");
tw.WriteLine(result.ToString()); //result is your string input
tw.Close();
ds.ReadXml("XMLString");

foreach(DataRow dr in ds.Tables[0].Rows)
{
Console.WriteLine(dr["put the element here"]); ......
}

Hope this helps!

Don't get me wrong, but this is incredibly awkward. There are numerous ways
to parse XML from a string without performing rain dances like that one.

Cheers,
 
GeRmIc said:
Hi Jooss,

I know, but does'nt this simple code gets the job done?

No, it's absolutely not simple. It writes the string back to disk, and then
uses an in-memory database for parsing the file.

No, it won't get the job done as expected. Put this code in a library and
try to reuse it from an ASP.NET application -- no joy, because the worker
process cannot write to its own virtual directory without changing
permissions.
You could
perhaps post some other ways to do it too so that the next person
could benefit from it.

You should have read the entire thread. XmlDocument.LoadXml() is all you
need.

Cheers,
 
Back
Top