System.Xml
-
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. -
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.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
-
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
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. -
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.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. TheCreateTextNode
method already takes the text of the text node as argument, so by passing in "Text" and afterwards setting theInnerText
property of the created node to "A text node" is redundant. Directly pass "A text node" to theCreateTextNode
method. 3. UsingSelectSingleNode
to get the root element of your document is quite inefficient. You can access the root element of a document directly via theXmlDocument.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
-
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. TheCreateTextNode
method already takes the text of the text node as argument, so by passing in "Text" and afterwards setting theInnerText
property of the created node to "A text node" is redundant. Directly pass "A text node" to theCreateTextNode
method. 3. UsingSelectSingleNode
to get the root element of your document is quite inefficient. You can access the root element of a document directly via theXmlDocument.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
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.
-
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.
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