Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. System.Xml.XmlDocument

System.Xml.XmlDocument

Scheduled Pinned Locked Moved Clever Code
helpdatabasexmltutorialquestion
10 Posts 4 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AlgorithmsBorne
    wrote on last edited by
    #1

    XmlDocument xmlDocument = new XmlDocument();
    //this throws an error
    try
    {
    xmlDocument.LoadXml("<root>this is unencoded inner text & < > </root>");
    }
    catch
    {
    }

    //Parses just fine
    xmlDocument.LoadXml("<root>this is encoded inner text & < ></root>");

    //sweet I don't have to think about encoding.........
    //because this will encode for me
    xmlDocument.DocumentElement.InnerText = "unencoded & < >";

    string encodedOuterXml = xmlDocument.OuterXml;
    //encodedOuterXml will be unencoded & < >

    //but what if I do think about encoding
    xmlDocument.DocumentElement.InnerText = "& < >";

    string outerXml = xmlDocument.OuterXml;
    //outerXml will be "&amp; &lt; &gt;"

    /*
    My point is that IMO inconsistent coding of a class is, in fact, a bug
    and that before encoding characters they need to check to see if it is already encoded

    it is the same all around the BCL and FCL.

    Another example of this is:
    *
    Uri.Query += "";

    the more you use += to append a query, the more ? it will add after the path
    */

    Enjoi

    S G 2 Replies Last reply
    0
    • A AlgorithmsBorne

      XmlDocument xmlDocument = new XmlDocument();
      //this throws an error
      try
      {
      xmlDocument.LoadXml("<root>this is unencoded inner text & < > </root>");
      }
      catch
      {
      }

      //Parses just fine
      xmlDocument.LoadXml("<root>this is encoded inner text & < ></root>");

      //sweet I don't have to think about encoding.........
      //because this will encode for me
      xmlDocument.DocumentElement.InnerText = "unencoded & < >";

      string encodedOuterXml = xmlDocument.OuterXml;
      //encodedOuterXml will be unencoded & < >

      //but what if I do think about encoding
      xmlDocument.DocumentElement.InnerText = "& < >";

      string outerXml = xmlDocument.OuterXml;
      //outerXml will be "&amp; &lt; &gt;"

      /*
      My point is that IMO inconsistent coding of a class is, in fact, a bug
      and that before encoding characters they need to check to see if it is already encoded

      it is the same all around the BCL and FCL.

      Another example of this is:
      *
      Uri.Query += "";

      the more you use += to append a query, the more ? it will add after the path
      */

      Enjoi

      S Offline
      S Offline
      Steve Hansen
      wrote on last edited by
      #2

      I don't believe the first behavior is a bug. The documentation clearly states that InnerText will be encoded. If you do want to encode yourself then just write to the InnerXml property. :)

      P 1 Reply Last reply
      0
      • S Steve Hansen

        I don't believe the first behavior is a bug. The documentation clearly states that InnerText will be encoded. If you do want to encode yourself then just write to the InnerXml property. :)

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Yeah, use the right tool for the right job.

        1 Reply Last reply
        0
        • A AlgorithmsBorne

          XmlDocument xmlDocument = new XmlDocument();
          //this throws an error
          try
          {
          xmlDocument.LoadXml("<root>this is unencoded inner text & < > </root>");
          }
          catch
          {
          }

          //Parses just fine
          xmlDocument.LoadXml("<root>this is encoded inner text & < ></root>");

          //sweet I don't have to think about encoding.........
          //because this will encode for me
          xmlDocument.DocumentElement.InnerText = "unencoded & < >";

          string encodedOuterXml = xmlDocument.OuterXml;
          //encodedOuterXml will be unencoded & < >

          //but what if I do think about encoding
          xmlDocument.DocumentElement.InnerText = "& < >";

          string outerXml = xmlDocument.OuterXml;
          //outerXml will be "&amp; &lt; &gt;"

          /*
          My point is that IMO inconsistent coding of a class is, in fact, a bug
          and that before encoding characters they need to check to see if it is already encoded

          it is the same all around the BCL and FCL.

          Another example of this is:
          *
          Uri.Query += "";

          the more you use += to append a query, the more ? it will add after the path
          */

          Enjoi

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          I don't understand what it is that you think is inconsistent?

          --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

          A 1 Reply Last reply
          0
          • G Guffa

            I don't understand what it is that you think is inconsistent?

            --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

            A Offline
            A Offline
            AlgorithmsBorne
            wrote on last edited by
            #5

            The implementation of the InnerText without taking into account the InnerXml. Basically the idea that you had to assign the InnerText invalid Xml to get properly formated output.

            G 1 Reply Last reply
            0
            • A AlgorithmsBorne

              The implementation of the InnerText without taking into account the InnerXml. Basically the idea that you had to assign the InnerText invalid Xml to get properly formated output.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              SharperAgent wrote:

              Basically the idea that you had to assign the InnerText invalid Xml to get properly formated output.

              You are not supposed to assign any XML to the InnerText property, you are supposed to assign text to it. That's why it's called InnerText and not InnerXml.

              --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

              A 1 Reply Last reply
              0
              • G Guffa

                SharperAgent wrote:

                Basically the idea that you had to assign the InnerText invalid Xml to get properly formated output.

                You are not supposed to assign any XML to the InnerText property, you are supposed to assign text to it. That's why it's called InnerText and not InnerXml.

                --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                A Offline
                A Offline
                AlgorithmsBorne
                wrote on last edited by
                #7

                Regardless it is accepting invalid XML and input. InnerText should not parse any of its contents into the tree, where as InnerXml should. But it all boils down to the fact that it accepts INVALID XML and doesn't validate the encoding if VALID XML is put into it, so it double encodes VALID XML. Enjoi

                P G 3 Replies Last reply
                0
                • A AlgorithmsBorne

                  Regardless it is accepting invalid XML and input. InnerText should not parse any of its contents into the tree, where as InnerXml should. But it all boils down to the fact that it accepts INVALID XML and doesn't validate the encoding if VALID XML is put into it, so it double encodes VALID XML. Enjoi

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Of course it does, XML is text. If you want it handled the same as non-XML text it must be set with InnerText, the same as other text. For example: If I have a text messaging program that uses XML something like <Message Sender="Me" Recipient="You">message text</Message> That "message text" is set with the InnerText property. If I send you a well-formed snippet of XML (e.g. <SomeData>Here's the data</SomeData>) I do not want to use InnerXML, I want the XML "text" to be encoded just like any other text. Correct, using InnerText: <Message Sender="Me" Recipient="You">&lt;SomeData&gt;Here's the data&lt;/SomeData&gt;</Message> Incorrect, using InnerXML: <Message Sender="Me" Recipient="You"><SomeData>Here's the data</SomeData></Message> Both are well-formed, but the latter is unlikely to be valid. It could be worse if I didn't use attributes (which may be a good reason to use attributes): <Message> <Sender>Me</Sender> <Recipient>You</Recipient> <Sender>GWB</Sender> </Message> Whoops, let's make that: <Message> <Sender>Me</Sender> <Recipient>You</Recipient> <Text> <Sender>GWB</Sender> </Text> </Message> Hopefully it will still fail validation, because if not GetElementsByTagName ( "Sender" ) will return both Sender elements.

                  1 Reply Last reply
                  0
                  • A AlgorithmsBorne

                    Regardless it is accepting invalid XML and input. InnerText should not parse any of its contents into the tree, where as InnerXml should. But it all boils down to the fact that it accepts INVALID XML and doesn't validate the encoding if VALID XML is put into it, so it double encodes VALID XML. Enjoi

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    If you want to choose InnerText or InnerXml depending on the well-formedness of the value, try something like: try { t.LoadXml ( args [ 0 ] ) ; e.InnerXml = args [ 0 ] ; } catch { e.InnerText = args [ 0 ] ; }

                    1 Reply Last reply
                    0
                    • A AlgorithmsBorne

                      Regardless it is accepting invalid XML and input. InnerText should not parse any of its contents into the tree, where as InnerXml should. But it all boils down to the fact that it accepts INVALID XML and doesn't validate the encoding if VALID XML is put into it, so it double encodes VALID XML. Enjoi

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      AlgorithmsBorne wrote:

                      But it all boils down to the fact that it accepts INVALID XML and doesn't validate the encoding if VALID XML is put into it, so it double encodes VALID XML.

                      No, it doesn't accept invalid XML. It accepts text, not XML. If the text happens to be XML is irrelevant, it should still treat it as text, and it does. If it would treat it as XML, the implementation would be inconsistent.

                      --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups