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