Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse XML string 2

Status
Not open for further replies.

jby1

Programmer
Apr 29, 2003
403
0
0
GB
Hi

I have what looks like a simple enough request.

I have a string in C#, which contains some XML.

I wish to parse out this XML string.

How can I do this in C#?

The string is something like:

Code:
string str = "<tag1>Tag 1 Value</tag1><tag2> Tag 2 Value</tag2>";
 
You need to create xsd schema for xml string. Then instance a new dataset and execute his two methods ReadXmlSchema and ReadXml. By the way your xml string is incomplete. tag1 and tag2 must be enclosed in some element like:
<tags>
<tag1>Tag 1 Value</tag1>
<tag2> Tag 2 Value</tag2>
</tags>
 
Hi

Thank you for you reply.

While I take your points about the form of the XML (I am pretty inexperienced in this area!), I don't think that the methods that you have suggested are doing to work for me. The ReadXml() method seems to require that I read the XML from a file or stream, I already have the XML in a string in C#, and want to parse it from there.

Any suggestions on the best approach here?

Thanks
 
Use the XMLDocument

Like this:

using System.Xml;

string test ="<root><node1>test</node1><node2>test2</node2>";

XmlDocument myDom = new XmlDocument();
myDom.Load(test);
string node1 = myDom.DocumentElement.SelectSingleNode("//node1").InnerText.ToString();


That should get you started. If you want to know more you should read up on xml and c#. It has some very nifty features.

Just do a search on google, it should give you plenty of good links.

Regards,
Stephan
 
Thanks again!

I have decided to use a different approach for now, although when I get the time I recognise that it is worthwhile learning this stuff properly.

As for searching Google, there is almost too much stuff, very hard to find what I actually want!

Thanks for your help though.
 
As for searching Google, there is almost too much stuff, very hard to find what I actually want!

I know what you are talking about!

:D
 
ReadXml have many overloads but the one you need is :
ReadXml(StreamReader)
here is an examble:
ds.ReadXml(new StringReader(strVariable));
 
Thank you!

I will give you a star for your wisdom :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top