Convert String Value into XML

J

Java Apache

Hi All,

I am pretty to new to .NET/C# so go easy!

I am developing a Web Service call to a Java Web Service and it is all
working fine - it is returning me back a String value which in all
reality is an XML file (the content of the string is a valid well-formed
XML packet). How can I get this string into an XML object so I can use
the XML functions / operations in .NET? An example of my XML String is
below:


<?xml version="1.0" encoding="UTF-8"?>
<NOVA version="1.0">
<PROCESSGUID>B1367940-802E-9665-4305EF47E9AC81F1</PROCESSGUID>
<REQUEST><USERNAME>foo</USERNAME><PASSWORD>foo</PASSWORD>
<COMPONENT>Test</COMPONENT>
<ARGUMENTS><address>earth</address><foo>earth</foo></ARGUMENTS>
<METHOD>foo</METHOD></REQUEST><RESPONSE><SUCCESS>true</SUCCESS><RESULT>I
worked!</RESULT><RESPONSEDATETIME>2006-07-27
18:19:35</RESPONSEDATETIME><ERROR>
<TYPE/><CODE/><MESSAGE/><DETAIL/></ERROR></RESPONSE></NOVA>


How could I get say, the "SUCCESS" value from this string

Hope some of you gurus can help ;-p

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

All you have to do is pass the string to the LoadXml method of the
XmlDocument class. Then, you can use XPath to get the values of the
elements/attributes in the document.

Hope this helps.
 
S

sloan

Here are some key words to get you going:

Namely, they are
XmlElement , XmlNodeList , XmlNode
methods:
SelectNodes
SelectSingleNode

If you go to
http://sholliday.spaces.msn.com/ 2/8/2006 entry
and download hte sample, you 'll find some xml code.

Below are some snipplets, they are *not* meant to work, just show some
syntax.




XmlElement root;

if (null!=root.Attributes["abc" ])
{
masterPortNumber = root.Attributes["abc"].Value ;

}

XmlNodeList myNL = root.SelectNodes("//someXpath/someChild1/someChild2);


if (myNL.Count <= 0)
{
throw new ArgumentException("No matches found ");
}

foreach (XmlNode xn in myNL )
{

}

Nicholas Paldino said:
All you have to do is pass the string to the LoadXml method of the
XmlDocument class. Then, you can use XPath to get the values of the
elements/attributes in the document.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Java Apache said:
Hi All,

I am pretty to new to .NET/C# so go easy!

I am developing a Web Service call to a Java Web Service and it is all
working fine - it is returning me back a String value which in all reality
is an XML file (the content of the string is a valid well-formed XML
packet). How can I get this string into an XML object so I can use the
XML functions / operations in .NET? An example of my XML String is below:


<?xml version="1.0" encoding="UTF-8"?>
<NOVA version="1.0">
<PROCESSGUID>B1367940-802E-9665-4305EF47E9AC81F1</PROCESSGUID>
<REQUEST><USERNAME>foo</USERNAME><PASSWORD>foo</PASSWORD>
<COMPONENT>Test</COMPONENT>
<ARGUMENTS><address>earth</address><foo>earth</foo></ARGUMENTS>
<METHOD>foo</METHOD></REQUEST><RESPONSE><SUCCESS>true</SUCCESS><RESULT>I
worked!</RESULT><RESPONSEDATETIME>2006-07-27
18:19:35</RESPONSEDATETIME><ERROR>
<TYPE/><CODE/><MESSAGE/><DETAIL/></ERROR></RESPONSE></NOVA>


How could I get say, the "SUCCESS" value from this string

Hope some of you gurus can help ;-p

Thanks
 
J

Java Apache

It does and it doesn't, I am not sure how to use the XPath part!? I have
this so far..

string novaResponse = testInvoke.externalRequest(sw.ToString());

NovaResponse is my XML string and it AOK.

I then have

xmlDoc.LoadXml(novaResponse); which is where I think you noted I
should be.....now for XPath? ;-p



All you have to do is pass the string to the LoadXml method of the
XmlDocument class. Then, you can use XPath to get the values of the
elements/attributes in the document.

Hope this helps.
a
 

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