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. XML / XSL
  4. Am I creating an XML document correctly?

Am I creating an XML document correctly?

Scheduled Pinned Locked Moved XML / XSL
csharpxmlquestion
2 Posts 2 Posters 3 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.
  • L Offline
    L Offline
    lnong
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • L lnong

      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.

      P Offline
      P Offline
      Philip Fitzsimons
      wrote on last edited by
      #2

      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 that AppendChild 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 use CloneNode: 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."

      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