2

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.

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer Richard. However this brings me to my next problem. I am using the 'Element' method to filter through the child elements. Since the XName isn't the simple name I expected it to be, I am not getting back any results. Any ideas?
Actually found it! XNamespace ns = XElement.Load(filename).GetDefaultNamespace(); Then I can use node.Element(ns + "ElementName").Value
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.