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" ?> 
  <order_date>2009-04-20 15:09:29</order_date> 
  <b_name1>Kris Gr</b_name1> 
  <b_address_line1>adres 1 adres 2</b_address_line1> 

Retrive the xml information contained within XML document using the url to the document itself (urlString):

             XmlDocument document = new XmlDocument();
             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()

    private void Form1_Load(object sender, EventArgs e)
    XmlDocument document = new XmlDocument();
    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;


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


Leave a Reply