Click here to Skip to main content
Click here to Skip to main content

Read XML with Namespace resolution using XLinq.XElement

By , 13 Nov 2008
Rate this:
Please Sign up or sign in to vote.

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
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

 
GeneralMy vote of 5 Pinmemberandrew.chudley26-Apr-11 22:04 
QuestionXML name space XNamespace PinmemberMember 343317322-Nov-10 19:07 
GeneralRe: XML name space XNamespace PinmemberAshish Mundra22-Nov-10 21:49 
Generalnot compiled with .Net 3.5 PinmemberMember 432304129-Dec-09 13:11 

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.

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