Today I will describe quick and dirty way to read xml data received as a web response. This can get very usefull if you want to send some information in a quck way without creating whole bunch of web service references etc.
Lets say I want to get some data from the server in the xml like this:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<order>
<order_id>100000001</order_id>
<order_date>2009-04-20 15:09:29</order_date>
<b_name1>Kris Gr</b_name1>
<b_name2>softberrieseshop</b_name2>
<b_address_line1>adres 1 adres 2</b_address_line1>
<b_city>Elblag</b_city>
<b_postcode>2341234</b_postcode>
<b_tel>65464564666</b_tel>
<b_fax>6546876465</b_fax>
<item>
<item_sku>DZFOREVER2</item_sku>
<qty_ordered>1.0000</qty_ordered>
<qty_invoiced>1.0000</qty_invoiced>
<qty_refunded>0.0000</qty_refunded>
<qty_shipped>0.0000</qty_shipped>
<qty_canceled>0.0000</qty_canceled>
</item>
</order>
</root>
Retrive the xml information contained within XML document using the url to the document itself (urlString):
XmlDocument document = new XmlDocument();
document.Load(urlString);
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/root/order");
foreach (XmlNode node in nodes)
{
string idStr = node["order_id"].InnerText;
string dateStr = node["order_date"].InnerText;
XmlElement root2 = doc.DocumentElement;
XmlNodeList nodes2 = root.SelectNodes("/root/order/item");
foreach (XmlNode nodex in nodes)
{
string sku = nodex["item_sku"].InnerText;
}
}
Simple and dead easy!