dopplesoldner
dopplesoldner

Reputation: 9479

Process namespaces using XmlReader

I have a complex XML file with structure as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="xxx:xxx:xxx:xxx:xxxxx:xxx:xsd:xxxx.xxx.xxx.xx">
    <Element1>
        <Element2>
            <Element2A>xxxxxx</Element2A>
            <Element2B>2012-08-29T00:00:00</Element2B>
        </Element2>
    </Element1>
</Document>

Now I am using XmlReader to read this XML document and process information as follows

XmlReader xr = XmlReader.Create(filename);
while (xr.Read()) 
{
   xr.MoveToElement();
   XElement node = (XElement)XElement.ReadFrom(xr);
   Console.WriteLine(node.Name);
}
xr.Close();

The problem I am facing is in the output the namespace is prefixed to the ElementName. E.g output

{xxx:xxx:xxx:xxx:xxxxx:xxx:xsd:xxxx.xxx.xxx.xx}Element1

Is there any way I can remove/ handle this as I need to do further filtering using Element names and Child names.

Upvotes: 2

Views: 3167

Answers (2)

Milind Thakkar
Milind Thakkar

Reputation: 980

You may want to remove the namespace. One way to remove namespace is to write c# code and other way is to use XSLT transformation as suggested in Remove Namespace -Milind

Upvotes: 0

Richard
Richard

Reputation: 109100

XElement.Name is not (as you might expect) a String, but rather an XName which has a LocalName property, thus:

Console.WriteLine(node.Name.LocalName);

Upvotes: 2

Related Questions