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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to read XML 1

Status
Not open for further replies.

MadJock

Programmer
Joined
May 25, 2001
Messages
318
Location
GB
Hi,

Sorry if this is a really basic question, I'm OK in C# but my XML is fairly basic!

I have an XML reply from an API that will resemble the following:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?><GV8APIREPLIES xmlns="gv8api-trayport-com"><REPLY Code="2" Message="Network Error - Can not connect to server." Reason=""/></GV8APIREPLIES>

I am able to get what I want (reply code and message) from the XML by loading it into a dataset myDataset.ReadXml(xmlString) and then looking at myDataset.Tables[0].Rows[0]["Code"], but am fairly certain this is not the best way to do it!

I also have an XSD document that fully describes the message (this message is a very basic example of what could be returned) but am unsure how to use it.

My current thoughts are I should probably be using XPath? Is this correct and can anyone provide an example?

Thanks in advance,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
Thanks Geert,

I'm not sure how to get the individual attributes of the REPLY node.

If I understand correctly, the XPath expression would be "GV8APIREPLIES/REPLY", but where do I go from there to get the code, message and reason?

Thanks for help,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
The code, message and reason are attributes of the node.

so if you did (pseudocode)
myString = "GV8APIREPLIES/REPLY" to get the reply node, then
"GV8APIREPLIES/REPLY@code" should get the code attribute contained within the reply node.

(check out for some basic info on xpath.)
 
Thanks to all for input and sorry if I'm being a muppet!

If I do:
Code:
XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xml);
            XmlNode node = xdoc.SelectSingleNode("GV8APIREPLIES/REPLY");

node is null.

If I do:
Code:
XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xml);
            XmlNode node = xdoc.SelectSingleNode("GV8APIREPLIES/REPLY@Code");

I get an invalid token exception.

Any suggestions?

Thanks,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
After you have done xDoc.LoadXml(xml) is there anything in the document?

K
 
Try this

Code:
string strXml = @"<?xml version=""1.0"" encoding=""ISO-8859-1""?><GV8APIREPLIES xmlns=""gv8api-trayport-com""><REPLY Code=""2"" Message=""Network Error - Can not connect to server."" Reason=""""/></GV8APIREPLIES>";

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(strXml);
	

XmlElement e = xDoc.DocumentElement;

//e.SelectSingleNode("GV8APIREPLIES");

XmlNode xn = e.SelectSingleNode("//@Code");

Initially you are trying to use XPath to interrogate an XmlDocument, not the actual XML, so you need to select the actual xml document first.

HtH

K
 

Kallisto,

Your a legend... have a start.

Thanks for your help,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 

Kallisto,

Your a legend... have a star.

Thanks for your help,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top