Click here to Skip to main content
Licence CPOL
First Posted 13 Nov 2008
Views 25,792
Bookmarked 4 times

Read XML with Namespace resolution using XLinq.XElement

By | 13 Nov 2008 | Article
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.

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)

About the Author

Ashish Mundra

Architect

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberandrew.chudley22:04 26 Apr '11  
QuestionXML name space XNamespace PinmemberMember 343317319:07 22 Nov '10  
GeneralRe: XML name space XNamespace PinmemberAshish Mundra21:49 22 Nov '10  
Generalnot compiled with .Net 3.5 PinmemberMember 432304113:11 29 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 13 Nov 2008
Article Copyright 2008 by Ashish Mundra
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid