XmlReader.ReadOuterXml() Returns Additional Namespace Info

D

dignan.tenenbaum

Hello,

I'm using the XmlReader.ReadOuterXml() method to return the string
representation of an xml node. The XmlReader is created with a file
path and a XmlReaderSettings object. This was working fine until a
default namespace was introduced. When the default namespace is
defined the ReadOuterXml() method began adding a bunch of extra
namespace declarations to the string it returned.

xml example ...

<?xml version="1.0" encoding="utf-8"?>
<declaration xmlns="http://www.w3schools.com" xmlns:xml-plus="http://
www.foo.com">

<xml-plus:unit id="1234">
Some text
<test xml-plus:fooTag="end">
in a tag.
</test>
</xml-plus:unit>

</declaration>

I want the outerxml to return this (which it does without a default
namespace defined) ...
<xml-plus:unit id="1234">
Some text
<test xml-plus:fooTag="end">
in a tag.
</test>
</xml-plus:unit>

But with a default namespace XmlReader returns this ...
<xml-plus:unit id="1234" xmlns:xml-plus="http://www.foo.com">
Some text
<test xml-plus:fooTag="end" xmlns="http://www.w3schools.com">
in a tag.
</test>
</xml-plus:unit>

This is a problem for me. My objective is to return a string which
represents the text of the original node.

After some research I discovered the XmlTextReader object. This has a
boolean property called "Namespaces". When Namespaces is set to true
it behaves the same way as the XmlReader - it adds the extraneous
namespace info. When Namespaces is set to false it behaves in the
manner I'd like - it does not include the additional namespaces but is
a true representation of the original xml.

I presented this information to my dev lead. He understood the
problem but did not want to use the XmlTextReader for a couple
reasons. One is that Microsoft recommends not using it. The other is
that our legacy code uses a XmlReaderSettings object when it
constructs the XmlReader. The constructor for XmlTextReader does not
except a XmlReaderSettings object so using it was deemed too risky.

My question is: does anyone know of a way to make the XmlReader behave
the same way as XmlTextReader when XmlTextReader.Namespaces is set to
false? Specifically the string returned by the ReadOuterXml method.

Thanks,
-Dignan
 
M

Martin Honnen

I'm using the XmlReader.ReadOuterXml() method to return the string
representation of an xml node. The XmlReader is created with a file
path and a XmlReaderSettings object. This was working fine until a
default namespace was introduced. When the default namespace is
defined the ReadOuterXml() method began adding a bunch of extra
namespace declarations to the string it returned.

xml example ...

<?xml version="1.0" encoding="utf-8"?>
<declaration xmlns="http://www.w3schools.com" xmlns:xml-plus="http://
www.foo.com">

The default namespace declaration applies to all descendants that have
no prefix so the 'test' element below is in the namespace
http://www.w3schools.com.
<xml-plus:unit id="1234">
Some text
<test xml-plus:fooTag="end">
in a tag.
</test>
</xml-plus:unit>

</declaration>

But with a default namespace XmlReader returns this ...
<xml-plus:unit id="1234" xmlns:xml-plus="http://www.foo.com">
Some text
<test xml-plus:fooTag="end" xmlns="http://www.w3schools.com">
in a tag.
</test>
</xml-plus:unit>

That is correct as that 'test' element is in the namespace
http://www.w3schools.com.
This is a problem for me. My objective is to return a string which
represents the text of the original node.

But the original node is in the namespace http://www.w3schools.com thus
any serialization trying to _represent_ that node needs to have the
namespace declaration.

You will need to write your own serialization code then that ignores
then namespaces of nodes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top