Reading XML from the web using C#

Reading XML from the web using C#

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!

One response on “Reading XML from the web using C#

  1. odalton February 27, 2011 at 11:25 pm

    cool example i am going to use this example combined with http request to read xml produced by php

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;

    namespace phptalk
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    XmlDocument document = new XmlDocument();
    document.Load(“http://127.0.0.1/csharp/index2.php”);
    XmlElement root = document.DocumentElement;
    XmlNodeList nodes = root.SelectNodes(“/root/order”);

    foreach (XmlNode node in nodes)
    {
    string idStr = node[“order_id”].InnerText;
    string dateStr = node[“order_date”].InnerText;

    MessageBox.Show(idStr);
    MessageBox.Show(dateStr);

    XmlElement root2 = document.DocumentElement;
    XmlNodeList nodes2 = root.SelectNodes(“/root/order/item”);
    foreach (XmlNode nodex in nodes2)
    {
    string sku = nodex[“item_sku”].InnerText;
    MessageBox.Show(sku);

    }
    }
    }
    }
    }

Leave a Reply