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. General Programming
  3. C#
  4. System.Xml

System.Xml

Scheduled Pinned Locked Moved C#
helpcsharpsecurityxmlperformance
6 Posts 2 Posters 0 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.
  • T Offline
    T Offline
    thecodedemon
    wrote on last edited by
    #1

    Using C# and .NET, I need to make an XML document. I've searched and all i can find is info on using XmlReader and XmlWriter, or XmlDocument after loading an already existing XML file. Problem is, I don't have or need a file. I really can't even make the file, due to security issues. I need to be able to make the XML structure in memory. Below is a sample of what I've tried so far, and the error it gives me. XmlDocument doc = new XmlDocument(); XmlNode node; // make a node node = doc.CreateTextNode( "This is a text node." ); // add it to the doc doc.AppendChild( node ); <-- System.InvalidOperationException was unhandled, "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type." I know I have all the right "using's", it compiles fine. My guess is the XmlDocument isn't initialized correctly, but i can't find info on how else to do so. Any help would be greatly appreciated. A soft glow comes from the pit in the darkness. The clicking noise become faster - and louder. A wind begins to stir up from the pit, as the creature flexes it's wings, preparing for flight. You stare into the pit, and hear a voice say in your mind, "If you survive the encounter, declare it to the world." The Code Demon Rises.

    S 1 Reply Last reply
    0
    • T thecodedemon

      Using C# and .NET, I need to make an XML document. I've searched and all i can find is info on using XmlReader and XmlWriter, or XmlDocument after loading an already existing XML file. Problem is, I don't have or need a file. I really can't even make the file, due to security issues. I need to be able to make the XML structure in memory. Below is a sample of what I've tried so far, and the error it gives me. XmlDocument doc = new XmlDocument(); XmlNode node; // make a node node = doc.CreateTextNode( "This is a text node." ); // add it to the doc doc.AppendChild( node ); <-- System.InvalidOperationException was unhandled, "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type." I know I have all the right "using's", it compiles fine. My guess is the XmlDocument isn't initialized correctly, but i can't find info on how else to do so. Any help would be greatly appreciated. A soft glow comes from the pit in the darkness. The clicking noise become faster - and louder. A wind begins to stir up from the pit, as the creature flexes it's wings, preparing for flight. You stare into the pit, and hear a voice say in your mind, "If you survive the encounter, declare it to the world." The Code Demon Rises.

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      A text node cannot be a direct child of an XML document. You have to create an element first that will become the root element of your document and add all other content to this element.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      T 1 Reply Last reply
      0
      • S Stefan Troschuetz

        A text node cannot be a direct child of an XML document. You have to create an element first that will become the root element of your document and add all other content to this element.


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        T Offline
        T Offline
        thecodedemon
        wrote on last edited by
        #3

        Ya, a few hours after I posted, after trying several different things, I figured it out. I'll post what I did, for anyone else having the same problem. XmlDocument doc = new XmlDocument(); XmlNode node1; XmlNode node2; // Initialize the Xml document with the root node. doc.AppendChild( doc.CreateNode( XmlNodeType.Element, "XMLRoot", string.Empty ) ); // make some more nodes // an element node node1 = doc.CreateNode( XmlNodeType.Element, "Element", null ); // a text node node2 = doc.CreateTextNode( "Text" ); node2.InnerText = "A text node"; // add the text node to the element node node1.AppendChild( node2 ); // and add the element node to the doc doc.SelectSingleNode( "XMLRoot" ).AppendChild( node1 ); doc.Save( Console.Out ); /************************** * produces the following output * * * < Element>A text node< /Element> * * ***************************/ Apparently, nodes can only be added to Element nodes. I haven't worked much with XML, so perhaps this is obvious to others. A soft glow comes from the pit in the darkness. The clicking noise become faster - and louder. A wind begins to stir up from the pit, as the creature flexes it's wings, preparing for flight. You stare into the pit, and hear a voice say in your mind, "If you survive the encounter, declare it to the world." The Code Demon Rises.

        S 1 Reply Last reply
        0
        • T thecodedemon

          Ya, a few hours after I posted, after trying several different things, I figured it out. I'll post what I did, for anyone else having the same problem. XmlDocument doc = new XmlDocument(); XmlNode node1; XmlNode node2; // Initialize the Xml document with the root node. doc.AppendChild( doc.CreateNode( XmlNodeType.Element, "XMLRoot", string.Empty ) ); // make some more nodes // an element node node1 = doc.CreateNode( XmlNodeType.Element, "Element", null ); // a text node node2 = doc.CreateTextNode( "Text" ); node2.InnerText = "A text node"; // add the text node to the element node node1.AppendChild( node2 ); // and add the element node to the doc doc.SelectSingleNode( "XMLRoot" ).AppendChild( node1 ); doc.Save( Console.Out ); /************************** * produces the following output * * * < Element>A text node< /Element> * * ***************************/ Apparently, nodes can only be added to Element nodes. I haven't worked much with XML, so perhaps this is obvious to others. A soft glow comes from the pit in the darkness. The clicking noise become faster - and louder. A wind begins to stir up from the pit, as the creature flexes it's wings, preparing for flight. You stare into the pit, and hear a voice say in your mind, "If you survive the encounter, declare it to the world." The Code Demon Rises.

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          Some remarks: 1. If you want to create an element why don't you use the CreateElement method. This way you do not need to pass a XmlNodeType value and furthermore the method as an overload where you do not need to specify a namespace URI (comes in handy for creating unqualified elements). 2. The CreateTextNode method already takes the text of the text node as argument, so by passing in "Text" and afterwards setting the InnerText property of the created node to "A text node" is redundant. Directly pass "A text node" to the CreateTextNode method. 3. Using SelectSingleNode to get the root element of your document is quite inefficient. You can access the root element of a document directly via the XmlDocument.DocumentElement property. As an alternative you could assign the root node to a variable after creating it and then append it (as done with the text node).


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          T 1 Reply Last reply
          0
          • S Stefan Troschuetz

            Some remarks: 1. If you want to create an element why don't you use the CreateElement method. This way you do not need to pass a XmlNodeType value and furthermore the method as an overload where you do not need to specify a namespace URI (comes in handy for creating unqualified elements). 2. The CreateTextNode method already takes the text of the text node as argument, so by passing in "Text" and afterwards setting the InnerText property of the created node to "A text node" is redundant. Directly pass "A text node" to the CreateTextNode method. 3. Using SelectSingleNode to get the root element of your document is quite inefficient. You can access the root element of a document directly via the XmlDocument.DocumentElement property. As an alternative you could assign the root node to a variable after creating it and then append it (as done with the text node).


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            T Offline
            T Offline
            thecodedemon
            wrote on last edited by
            #5

            I can see what you're saying. Certainly the code I posted an be cleaned up and made more efficient. I was working on this last night, and what I posted was written at 1 in the morning. It worked, gave me the output I was looking for, I was happy, and went to bed. I'll clean it up later, and let you know if I see anything other than what you mentioned. Thanks for the help though.

            A soft glow comes from the pit in the darkness. The clicking noise become faster - and louder. A wind begins to stir up from the pit, as the creature flexes it's wings, preparing for flight. You stare into the pit, and hear a voice say in your mind, "If you survive the encounter, declare it to the world." The Code Demon Rises.

            S 1 Reply Last reply
            0
            • T thecodedemon

              I can see what you're saying. Certainly the code I posted an be cleaned up and made more efficient. I was working on this last night, and what I posted was written at 1 in the morning. It worked, gave me the output I was looking for, I was happy, and went to bed. I'll clean it up later, and let you know if I see anything other than what you mentioned. Thanks for the help though.

              A soft glow comes from the pit in the darkness. The clicking noise become faster - and louder. A wind begins to stir up from the pit, as the creature flexes it's wings, preparing for flight. You stare into the pit, and hear a voice say in your mind, "If you survive the encounter, declare it to the world." The Code Demon Rises.

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              I'm well aware that during the process of learning one does not always find the best way at first and is happy to find a way that works anyhow. So my posting was not intended to criticize you. Just wanted to give you some pointers what can be optimized.


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              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