Lack of understanding about the XmlDocument (C#)
-
I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:
XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");This is the code the developer wrote.
XmlElement bookElement = target.CreateElement("BOOK");
targetNode = targetNode.AppendChild(bookElement);
targetNode.InnerXml = sourceNode.InnerXml;How performant do you think it would be compared to this?
XmlNode importedNode = target.ImportNode(sourceNode, true);
targetNode.AppendChild(importedNode);I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!
-
I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:
XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");This is the code the developer wrote.
XmlElement bookElement = target.CreateElement("BOOK");
targetNode = targetNode.AppendChild(bookElement);
targetNode.InnerXml = sourceNode.InnerXml;How performant do you think it would be compared to this?
XmlNode importedNode = target.ImportNode(sourceNode, true);
targetNode.AppendChild(importedNode);I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!
Yes, I assume so. As once the node is created it's stored in memory ready to be attached to another node, whereas if the XML is "stringified" then I assume the text is parsed, the node generated and copied to memory and then attached. The other obvious advantage is less code :cool: Regards, --Perspx
"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:
XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");This is the code the developer wrote.
XmlElement bookElement = target.CreateElement("BOOK");
targetNode = targetNode.AppendChild(bookElement);
targetNode.InnerXml = sourceNode.InnerXml;How performant do you think it would be compared to this?
XmlNode importedNode = target.ImportNode(sourceNode, true);
targetNode.AppendChild(importedNode);I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!
At least he was trying to use Xml. I'm sick and tired of seeing questions where people are looking for nodes in an Xml document and just treating the whole thing as one big string.
Deja View - the feeling that you've seen this post before.
-
At least he was trying to use Xml. I'm sick and tired of seeing questions where people are looking for nodes in an Xml document and just treating the whole thing as one big string.
Deja View - the feeling that you've seen this post before.
-
At least we CAN blame VB for this one!
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
Yes, I remember in VB6 days when you had an MSXML2 and MSXML4 documents, stringifying the XML was the only way to copy data across from one to another. The MSXML parser didn't like you mixing different versions.
DrWheetos wrote:
Yes, I remember in VB6 days when you had an MSXML2 and MSXML4 documents
Never, ever admit to remembering VB6 days. Claim you had amnesia or something instead. You'll only get tarred with a brush you don't want.
Deja View - the feeling that you've seen this post before.
-
I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:
XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");This is the code the developer wrote.
XmlElement bookElement = target.CreateElement("BOOK");
targetNode = targetNode.AppendChild(bookElement);
targetNode.InnerXml = sourceNode.InnerXml;How performant do you think it would be compared to this?
XmlNode importedNode = target.ImportNode(sourceNode, true);
targetNode.AppendChild(importedNode);I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!
Yes, it would be a bit on the inefficient side. :rolleyes:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:
XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");This is the code the developer wrote.
XmlElement bookElement = target.CreateElement("BOOK");
targetNode = targetNode.AppendChild(bookElement);
targetNode.InnerXml = sourceNode.InnerXml;How performant do you think it would be compared to this?
XmlNode importedNode = target.ImportNode(sourceNode, true);
targetNode.AppendChild(importedNode);I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!
Yes! buddy :-\