How to parse XML-string ?

  • Thread starter Thread starter MichalSody
  • Start date Start date
M

MichalSody

Could someboby help me please with this simple problem ?

I've got a resultvalue as string in XML-format:
string result = <?xml version="1.0" encoding="ISO-8859-1"
?><HITLIST><KEYSIZE>7</KEYSIZE><NKEYS>0000000007</NKEYS><KEYSET>0011028001178200185320027592002762800278510033581</KEYSET><STATUS>READY</STATUS></HITLIST>

and I would like to extract these values:
KEYSIZE - that number 7
NKEYS - that number 7
and KEYSET - whole string with ID's

is there a better way than do this with string operations like substring ?
 
Could someboby help me please with this simple problem ?

I've got a resultvalue as string in XML-format:
string result = <?xml version="1.0" encoding="ISO-8859-1"
?><HITLIST><KEYSIZE>7</KEYSIZE><NKEYS>0000000007</NKEYS><KEYSET>0011028001178200185320027592002762800278510033581</KEYSET><STATUS>READY</STATUS></HITLIST>

and I would like to extract these values:
KEYSIZE - that number 7
NKEYS - that number 7
and KEYSET - whole string with ID's

is there a better way than do this with string operations like substring ?

If it's XML, you should use XML handling classes - load it into an
XmlDocument (using XmlDocument.LoadXml) and navigate it appropriately
(e.g. SelectSingleNode etc).

Jon
 
Hi Michal,

Another example:

XPathDocument document = new XPathDocument(new StringReader(<your_doc>));
XPathNavigator xn = document.CreateNavigator();

xn.SelectSingleNode(@"/HITLIST/NKEYS").ValueAsInt; // 7
xn.SelectSingleNode(@"/HITLIST/KEYSIZE").ValueAsInt; // 7
xn.SelectSingleNode(@"/HITLIST/KEYSET").Value; // <string_value>

Regards, Alex
[TechBlog] http://devkids.blogspot.com
 

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

Back
Top