Click here to Skip to main content
13,097,884 members (53,930 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 13 Nov 2008

Read XML with Namespace resolution using XLinq.XElement

, 13 Nov 2008
Rate this:
Please Sign up or sign in to vote.
Read XML with Namespace resolution using XLinq.XElement


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.


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.

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

namespace XElementReading
    class Program
        static string myxml =
                <one xmlns:a=''>
                    <a:oneone xmlns:b=''>
                    <a:twotwo xmlns:b=''>

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

            XNamespace nsr = "";
            XNamespace nsy = "";

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

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

Points of Interest

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




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


About the Author

Ashish Mundra
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.

You may also be interested in...


Comments and Discussions

QuestionGet namespace from XElement Pin
Alex88-November23-Jul-14 2:57
memberAlex88-November23-Jul-14 2:57 
GeneralMy vote of 5 Pin
andrew.chudley26-Apr-11 22:04
memberandrew.chudley26-Apr-11 22:04 
QuestionXML name space XNamespace Pin
Member 343317322-Nov-10 19:07
memberMember 343317322-Nov-10 19:07 
GeneralRe: XML name space XNamespace Pin
Ashish Mundra22-Nov-10 21:49
memberAshish Mundra22-Nov-10 21:49 
Generalnot compiled with .Net 3.5 Pin
Member 432304129-Dec-09 13:11
memberMember 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 13 Nov 2008
Article Copyright 2008 by Ashish Mundra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid