InnerXml and PreserveWhitespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFyaw==?=

    InnerXml and PreserveWhitespace

    Hi...

    I just noticed something that seems counter-intuitive to me. By default an
    XmlDocument is set with PreserveWhitesp ace=false. This means that
    XmlDocument.Loa d() or .LoadXml() will strip/condense non-semantic whitespace.

    *But* I just found that if you take that self-same XmlDocument and do either
    XmlDocumentFrag ment node = doc.CreateDocum entFragment();
    node.InnerXml = "<foo/>\r\n<bar/>";

    or
    XmlElement node = doc.CreateEleme nt("baz");
    node.InnerXml = "<foo/>\r\n<bar/>";

    that the whitespace gets preserved, despite the parent document settings.

    What's the rationale for this?

    Thanks
    Mark


  • Joe Fawcett

    #2
    Re: InnerXml and PreserveWhitesp ace



    "Mark" <mmodrall@nospa m.nospamwrote in message
    news:6895BFB9-C5E1-45DF-AF83-CE9246217535@mi crosoft.com...
    Hi...
    >
    I just noticed something that seems counter-intuitive to me. By default
    an
    XmlDocument is set with PreserveWhitesp ace=false. This means that
    XmlDocument.Loa d() or .LoadXml() will strip/condense non-semantic
    whitespace.
    >
    *But* I just found that if you take that self-same XmlDocument and do
    either
    XmlDocumentFrag ment node = doc.CreateDocum entFragment();
    node.InnerXml = "<foo/>\r\n<bar/>";
    >
    or
    XmlElement node = doc.CreateEleme nt("baz");
    node.InnerXml = "<foo/>\r\n<bar/>";
    >
    that the whitespace gets preserved, despite the parent document settings.
    >
    What's the rationale for this?
    >
    Thanks
    Mark
    >
    >
    The whole thing is odd. The standards state that preserve is the default,
    this is one of the few areas that Microsoft deviate from the standard in
    regards to the Xml DOM.

    --

    Joe Fawcett (MVP - XML)


    Comment

    • Martin Honnen

      #3
      Re: InnerXml and PreserveWhitesp ace

      Mark wrote:
      I just noticed something that seems counter-intuitive to me. By default an
      XmlDocument is set with PreserveWhitesp ace=false. This means that
      XmlDocument.Loa d() or .LoadXml() will strip/condense non-semantic whitespace.
      >
      *But* I just found that if you take that self-same XmlDocument and do either
      XmlDocumentFrag ment node = doc.CreateDocum entFragment();
      node.InnerXml = "<foo/>\r\n<bar/>";
      >
      or
      XmlElement node = doc.CreateEleme nt("baz");
      node.InnerXml = "<foo/>\r\n<bar/>";
      >
      that the whitespace gets preserved, despite the parent document settings.
      >
      What's the rationale for this?
      I am not sure but OuterXml/InnerXml never pay attention to the
      PreserveWhitesp ace property, neither on reading nor on setting.

      Here is an example showing that for reading OuterXml and comparing it to
      the Save method:

      XmlDocument doc = new XmlDocument();
      doc.LoadXml("<r oot><foo><bar>b az</bar></foo></root>");
      doc.Save(Consol e.Out);
      Console.WriteLi ne();
      Console.WriteLi ne(doc.OuterXml );
      doc.PreserveWhi tespace = true;
      doc.Save(Consol e.Out);
      Console.WriteLi ne();
      Console.WriteLi ne(doc.OuterXml );

      Output is

      <?xml version="1.0" encoding="ibm85 0"?>
      <root>
      <foo>
      <bar>baz</bar>
      </foo>
      </root>
      <root><foo><bar >baz</bar></foo></root>
      <?xml version="1.0"
      encoding="ibm85 0"?><root><foo> <bar>baz</bar></foo></root>
      <root><foo><bar >baz</bar></foo></root>



      --

      Martin Honnen --- MVP XML

      Comment

      • =?Utf-8?B?TWFyaw==?=

        #4
        Re: InnerXml and PreserveWhitesp ace

        Thanks for the reply, Martin...

        The interesting wrinkle in your sample is that .Save() actually injects
        whitespace when PreserveWhitesp ace = false. The original string you parsed
        had no non-semantic whitespace in it, yet the first .Save() applied
        formatting to add some.

        My next experiment was going to try making an XmlTextReader using
        XmlParserContex t to see how that interacted with whitespace...

        Mark


        "Martin Honnen" wrote:
        Mark wrote:
        >
        I just noticed something that seems counter-intuitive to me. By default an
        XmlDocument is set with PreserveWhitesp ace=false. This means that
        XmlDocument.Loa d() or .LoadXml() will strip/condense non-semantic whitespace.

        *But* I just found that if you take that self-same XmlDocument and do either
        XmlDocumentFrag ment node = doc.CreateDocum entFragment();
        node.InnerXml = "<foo/>\r\n<bar/>";

        or
        XmlElement node = doc.CreateEleme nt("baz");
        node.InnerXml = "<foo/>\r\n<bar/>";

        that the whitespace gets preserved, despite the parent document settings.

        What's the rationale for this?
        >
        I am not sure but OuterXml/InnerXml never pay attention to the
        PreserveWhitesp ace property, neither on reading nor on setting.
        >
        Here is an example showing that for reading OuterXml and comparing it to
        the Save method:
        >
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<r oot><foo><bar>b az</bar></foo></root>");
        doc.Save(Consol e.Out);
        Console.WriteLi ne();
        Console.WriteLi ne(doc.OuterXml );
        doc.PreserveWhi tespace = true;
        doc.Save(Consol e.Out);
        Console.WriteLi ne();
        Console.WriteLi ne(doc.OuterXml );
        >
        Output is
        >
        <?xml version="1.0" encoding="ibm85 0"?>
        <root>
        <foo>
        <bar>baz</bar>
        </foo>
        </root>
        <root><foo><bar >baz</bar></foo></root>
        <?xml version="1.0"
        encoding="ibm85 0"?><root><foo> <bar>baz</bar></foo></root>
        <root><foo><bar >baz</bar></foo></root>
        >
        >
        >
        --
        >
        Martin Honnen --- MVP XML

        >

        Comment

        • =?Utf-8?B?TWFyaw==?=

          #5
          Re: InnerXml and PreserveWhitesp ace

          Okay, I did find that you can get a fragment without whitespace using
          XmlTextReader.

          Interestingly, setting XmlParserContex t with various XmlSpace values doesn't
          seem to change the behavior of the reader at all.

          *But* you can fiddle the XmlTextReader.W hitespaceHandli ng value after
          construction to change how it reads things.

          I was intrigued by the WhitespaceHandl ing.None option (separate from the
          ..Significant option); some whitespace removal would materially change the
          meaning of the document, so it seemed like an odd extra to be able to say
          "rip it out even if it changes the underlying meaning"...

          Thanks
          Mark

          Comment

          Working...