XmlReader.ReadOuterXml() Returns Additional Namespace Info

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dignan.tenenbaum@gmail.com

    #1

    XmlReader.ReadOuterXml() Returns Additional Namespace Info


    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
  • Martin Honnen

    #2
    Re: XmlReader.ReadO uterXml() Returns Additional Namespace Info

    dignan.tenenbau m@gmail.com wrote:
    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">
    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="en d">
    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="en d" xmlns="http://www.w3schools.c om">
    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.




    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

    Comment

    Working...