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. XmlDocument Write problems and Add Identation Help!

XmlDocument Write problems and Add Identation Help!

Scheduled Pinned Locked Moved C#
csharpcomxmlhelpquestion
4 Posts 3 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.
  • C Offline
    C Offline
    Chua Wen Ching
    wrote on last edited by
    #1

    Hi there, I have 2 questions to ask. 1) I have this code... XmlDocument doc = new XmlDocument(); doc.Load(myXmlPath); int maxValue = 0; int newValue = 0; XmlNodeList nodeList = doc.SelectNodes("//Product"); foreach (XmlNode node in nodeList) maxValue = XmlConvert.ToInt32(node.Attributes["id"].Value); XmlElement newElem = doc.CreateElement("Product"); newValue = maxValue + 1; XmlAttribute newAttr = doc.CreateAttribute("id"); newAttr.Value = newValue.ToString(); newElem.Attributes.Append(newAttr); doc.DocumentElement.AppendChild(newElem); XmlElement elem = doc.CreateElement("Description"); elem.InnerText = "JVC Camera"; doc.DocumentElement.AppendChild(elem); XmlTextWriter wrtr = new XmlTextWriter(myXmlPath, Encoding.UTF8); doc.WriteTo(wrtr); wrtr.Close(); Xml (Before) // xml has identation nicely ---------------- Panasonic Camcorder Xml(After) // xml has no identation -------------- Panasonic Camcorder JVC Camera Xml (Expected this) ----------------------- Panosonic Camcorder JVC Camera Note: I want the Second Description tag to be part of Product id="2", but with my C# codes, i can't achieve it. 2) After i perform the C# code on top, my codes has no more identation. It seems that it removes all the whitespaces to this. (Please take note, the above xml examples, i manually re-arrange the generated xml for clear view). Xml (After) -------------- Panasonic CamcorderJVC Camera I hope i can indent nicely, just like what i did above on original xml file. Any help please? -- Regards, Chua Wen Ching Visit us at http://www.necoders.com Regards, Chua Wen Ching Visit us at http://www.necoders.com

    S J 2 Replies Last reply
    0
    • C Chua Wen Ching

      Hi there, I have 2 questions to ask. 1) I have this code... XmlDocument doc = new XmlDocument(); doc.Load(myXmlPath); int maxValue = 0; int newValue = 0; XmlNodeList nodeList = doc.SelectNodes("//Product"); foreach (XmlNode node in nodeList) maxValue = XmlConvert.ToInt32(node.Attributes["id"].Value); XmlElement newElem = doc.CreateElement("Product"); newValue = maxValue + 1; XmlAttribute newAttr = doc.CreateAttribute("id"); newAttr.Value = newValue.ToString(); newElem.Attributes.Append(newAttr); doc.DocumentElement.AppendChild(newElem); XmlElement elem = doc.CreateElement("Description"); elem.InnerText = "JVC Camera"; doc.DocumentElement.AppendChild(elem); XmlTextWriter wrtr = new XmlTextWriter(myXmlPath, Encoding.UTF8); doc.WriteTo(wrtr); wrtr.Close(); Xml (Before) // xml has identation nicely ---------------- Panasonic Camcorder Xml(After) // xml has no identation -------------- Panasonic Camcorder JVC Camera Xml (Expected this) ----------------------- Panosonic Camcorder JVC Camera Note: I want the Second Description tag to be part of Product id="2", but with my C# codes, i can't achieve it. 2) After i perform the C# code on top, my codes has no more identation. It seems that it removes all the whitespaces to this. (Please take note, the above xml examples, i manually re-arrange the generated xml for clear view). Xml (After) -------------- Panasonic CamcorderJVC Camera I hope i can indent nicely, just like what i did above on original xml file. Any help please? -- Regards, Chua Wen Ching Visit us at http://www.necoders.com Regards, Chua Wen Ching Visit us at http://www.necoders.com

      S Offline
      S Offline
      Stanimir_Stoyanov
      wrote on last edited by
      #2

      I use this way: after the XmlTextWriter initialization put this code: wrtr.Formatting = Formatting.Indented; so the code block looks like this XmlTextWriter wrtr = new XmlTextWriter(myXmlPath, Encoding.UTF8); wrtr.Formatting = Formatting.Indented; stan_fisherâ„¢ [www.aeroxp.net]

      1 Reply Last reply
      0
      • C Chua Wen Ching

        Hi there, I have 2 questions to ask. 1) I have this code... XmlDocument doc = new XmlDocument(); doc.Load(myXmlPath); int maxValue = 0; int newValue = 0; XmlNodeList nodeList = doc.SelectNodes("//Product"); foreach (XmlNode node in nodeList) maxValue = XmlConvert.ToInt32(node.Attributes["id"].Value); XmlElement newElem = doc.CreateElement("Product"); newValue = maxValue + 1; XmlAttribute newAttr = doc.CreateAttribute("id"); newAttr.Value = newValue.ToString(); newElem.Attributes.Append(newAttr); doc.DocumentElement.AppendChild(newElem); XmlElement elem = doc.CreateElement("Description"); elem.InnerText = "JVC Camera"; doc.DocumentElement.AppendChild(elem); XmlTextWriter wrtr = new XmlTextWriter(myXmlPath, Encoding.UTF8); doc.WriteTo(wrtr); wrtr.Close(); Xml (Before) // xml has identation nicely ---------------- Panasonic Camcorder Xml(After) // xml has no identation -------------- Panasonic Camcorder JVC Camera Xml (Expected this) ----------------------- Panosonic Camcorder JVC Camera Note: I want the Second Description tag to be part of Product id="2", but with my C# codes, i can't achieve it. 2) After i perform the C# code on top, my codes has no more identation. It seems that it removes all the whitespaces to this. (Please take note, the above xml examples, i manually re-arrange the generated xml for clear view). Xml (After) -------------- Panasonic CamcorderJVC Camera I hope i can indent nicely, just like what i did above on original xml file. Any help please? -- Regards, Chua Wen Ching Visit us at http://www.necoders.com Regards, Chua Wen Ching Visit us at http://www.necoders.com

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        As for part 2 of your question, you are appending both new nodes to the doc.DocumentElement node whereas what you want is to create the Product node, append that to the DocumentElement and then append the description node as a child to the newly created Product node.

        C 1 Reply Last reply
        0
        • J J4amieC

          As for part 2 of your question, you are appending both new nodes to the doc.DocumentElement node whereas what you want is to create the Product node, append that to the DocumentElement and then append the description node as a child to the newly created Product node.

          C Offline
          C Offline
          Chua Wen Ching
          wrote on last edited by
          #4

          Hi, Thanks Stanimir_Stoyanov and J4amieC. I got it right already with your suggestions. Cheers. Regards, Chua Wen Ching Visit us at http://www.necoders.com

          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