Parsing XML from a String

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
 
G

Guest

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

Regards,
Amal
 
A

Abhishek Srivastava

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

Guest

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
 
G

Guest

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!
 
J

Joerg Jooss

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,
 
G

Guest

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,
 
J

Joerg Jooss

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top