Hello,
I'm using the XmlReader.ReadO uterXml() method to return the string
representation of an xml node. The XmlReader is created with a file
path and a XmlReaderSettin gs 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.c om" xmlns:xml-plus="http://
www.foo.com">
<xml-plus:unit id="1234">
Some text
<test xml-plus:fooTag="en d">
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="en d">
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="en d" xmlns="http://www.w3schools.c om">
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 XmlReaderSettin gs object when it
constructs the XmlReader. The constructor for XmlTextReader does not
except a XmlReaderSettin gs 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.N amespaces is set to
false? Specifically the string returned by the ReadOuterXml method.
Thanks,
-Dignan
Comment