Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / C#
Article

Read XML with Namespace resolution using XLinq.XElement

Rate me:
Please Sign up or sign in to vote.
2.67/5 (11 votes)
13 Nov 2008CPOL 72.7K   5   5
Read XML with Namespace resolution using XLinq.XElement

Introduction

This article demonstrates how to read a xml which has several namespaces via XElement. This is a easy day to day activity which is not so obvious, so writing a small snippet for helping developers.

Background

XML Namespaces are used to identify a domain for a tag. So if a xml is a mix of xml's from two different places and have same tag name, then parsers can understand that the tag belongs to which domain. In this article we will help parser by specifying the namespace while reading.

Using the code

After reading the background, code should be very easy to understand. If we do not supply a namespace to obtain element, parser would not be able to understand the tag's origin and so will not be able to obtain the result. If we are reading a XML which has a namespace then we must resolve the same.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace XElementReading
{
    class Program
    {
        static string myxml =
            @"<root>
                <one xmlns:a='http://rediff.com'>
                    <a:oneone xmlns:b='http://yahoo.com'>
                        <b:id>1</b:id>
                        <b:name></b:name>
                    </a:oneone>
                    <a:twotwo xmlns:b='http://orkut.com'>
                        <b:id>1</b:id>
                        <b:name></b:name>
                    </a:twotwo>
                </one>
              </root>";

        static void Main(string[] args)
        {
            XElement elem = XElement.Parse(myxml);

            XNamespace nsr = "http://rediff.com";
            XNamespace nsy = "http://yahoo.com";

            string t = elem.Element("one").Element(nsr + "oneone").Element(nsy + "id").Value;

	    Console.WriteLine("Value of id within oneone: {0}", t);	
            Console.ReadLine();
        }
    }
}

Points of Interest

This snippet helps us understand the necessity of namespaces and XML parsers need for a namespace.

History

None.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Ashish has 9 years of software development experience on Microsoft Technology. He is a MCSD .Net 1.1 and TS - SQL Server 2005 and TS - WCF

He has worked on both windows and web based applications and participated in various phases of Software Development Life Cycle.

Comments and Discussions

 
QuestionGet namespace from XElement Pin
Alexcei8823-Jul-14 2:57
Alexcei8823-Jul-14 2:57 
GeneralMy vote of 5 Pin
andrew.chudley26-Apr-11 22:04
professionalandrew.chudley26-Apr-11 22:04 
QuestionXML name space XNamespace Pin
Member 343317322-Nov-10 19:07
Member 343317322-Nov-10 19:07 
GeneralRe: XML name space XNamespace Pin
Ashish Mundra22-Nov-10 21:49
Ashish Mundra22-Nov-10 21:49 
Generalnot compiled with .Net 3.5 Pin
Member 432304129-Dec-09 13:11
Member 432304129-Dec-09 13:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.