3

I am trying to read inner XML of the root element as string but XmlReader class is namespace aware. Is there any way to ignore the namespaces? I can use any class in BCL as long as it passes the NUNit test below.

[Test]
public void GetInnerXmlTest()
{
    string subject = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                     "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" +
                         "<url><loc>abc</loc></url>" +
                         "<url><loc>def</loc></url>" +
                     "</urlset>";

    XDocument document = XDocument.Parse(subject);
    XmlReader xmlReader = document.FirstNode.CreateReader();
    xmlReader.MoveToContent();
    string innerXml = xmlReader.ReadInnerXml();
    //<url xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><loc>abc</loc></url><url xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><loc>def</loc></url>

    Assert.AreEqual("<url><loc>abc</loc></url><url><loc>def</loc></url>",
                    innerXml);
}
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • Ok then, what result do you get from `ReadInnerXml` and why do you think it's wrong? – Ondrej Tucny Jan 01 '14 at 00:24
  • 1
    @OndrejTucny I updated the question with the value returned from ReadInnerXml method. You can see the output I want to get in the assertion. – Ufuk Hacıoğulları Jan 01 '14 at 00:28
  • 1
    these questions maybe helpful: http://stackoverflow.com/questions/4440451/how-to-ignore-namespaces-with-xpath http://stackoverflow.com/questions/4402310/how-to-ignore-namespace-when-selecting-xml-nodes-with-xpath – Selman Genç Jan 01 '14 at 00:38

1 Answers1

0

You have to remove the namespace first. How to remove all namespaces from XML with C#?

You cant just take it out after you have used innerXML.

Community
  • 1
  • 1
Softwarehuset
  • 815
  • 4
  • 10