Am I creating an XML document correctly?
-
The C# code I'm using is shown below. The output file looks almost exactly how a basic XML file should look, I believe. My only concern is that how come the code to create it looks rather inefficient? How come I need to do "folder = doc.CreateElement("folder")" for EVERY new node that I create?? Can't I just use the "folder" object as a template for a folder element? Shouldnt all I need to do is set the attribute value and the inner text, and then reuse "folder" element object to append the new node? This is what I am saying:
XmlElement folder = doc.CreateElement("folder"); // Add first node folder.SetAttribute("name", "folder 1"); folder.InnerText = "1st Node"; root.AppendChild(folder); // Add second node folder.SetAttribute("name", "folder 2"); folder.InnerText = "2nd Node"; root.AppendChild(folder);
However, that did not work. Here's the (whole) code that does work for me:XmlDataDocument doc = new XmlDataDocument(); XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); doc.InsertAfter(declaration, null); XmlElement root = doc.CreateElement("file_system"); doc.InsertAfter(root, declaration); XmlElement folder; // Add first node folder = doc.CreateElement("folder"); folder.SetAttribute("name", "folder 1"); folder.InnerText = "1st Node"; root.AppendChild(folder); // Add second node folder = doc.CreateElement("folder"); folder.SetAttribute("name", "folder 2"); folder.InnerText = "2nd Node"; root.AppendChild(folder); // Output to file XmlTextWriter writer = new XmlTextWriter(@"d:\XMLTest.xml", null); doc.WriteTo(writer); writer.Close();
Is this the correct and most efficient way to create an XML document dynamically? Please share with me better solutions. Thanks. -
The C# code I'm using is shown below. The output file looks almost exactly how a basic XML file should look, I believe. My only concern is that how come the code to create it looks rather inefficient? How come I need to do "folder = doc.CreateElement("folder")" for EVERY new node that I create?? Can't I just use the "folder" object as a template for a folder element? Shouldnt all I need to do is set the attribute value and the inner text, and then reuse "folder" element object to append the new node? This is what I am saying:
XmlElement folder = doc.CreateElement("folder"); // Add first node folder.SetAttribute("name", "folder 1"); folder.InnerText = "1st Node"; root.AppendChild(folder); // Add second node folder.SetAttribute("name", "folder 2"); folder.InnerText = "2nd Node"; root.AppendChild(folder);
However, that did not work. Here's the (whole) code that does work for me:XmlDataDocument doc = new XmlDataDocument(); XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); doc.InsertAfter(declaration, null); XmlElement root = doc.CreateElement("file_system"); doc.InsertAfter(root, declaration); XmlElement folder; // Add first node folder = doc.CreateElement("folder"); folder.SetAttribute("name", "folder 1"); folder.InnerText = "1st Node"; root.AppendChild(folder); // Add second node folder = doc.CreateElement("folder"); folder.SetAttribute("name", "folder 2"); folder.InnerText = "2nd Node"; root.AppendChild(folder); // Output to file XmlTextWriter writer = new XmlTextWriter(@"d:\XMLTest.xml", null); doc.WriteTo(writer); writer.Close();
Is this the correct and most efficient way to create an XML document dynamically? Please share with me better solutions. Thanks.yep thats pretty much the best way to do it. the reason you need to create and instance of folder (
CreateFolder
) for each item is thatAppendChild
is appending a reference to the folder, rather than copying it. the way you could speed/simply life is to create a template node and useCloneNode
:XmlElement templateFolder = doc.CreateElement("folder"); XmlElement folder; // Add first node folder = templateFolder.CloneNode(); folder.SetAttribute("name", "folder 1"); folder.InnerText = "1st Node"; root.AppendChild(folder); // Add second node folder = templateFolder.CloneNode(); folder.SetAttribute("name", "folder 2"); folder.InnerText = "2nd Node"; root.AppendChild(folder);
hope this helps
"When the only tool you have is a hammer, a sore thumb you will have."