Adding XML comment to large number of existing XML Files?
I have several XML files, and i want to add an XML comment at the top of each file using C#.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Master BillaRank #2,229 · 630 reputationPosted Sep 19, 2009, 7:05 PM EDT
You can do this code
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("");
XmlNode node = doc.CreateNode("Comments", "Comments", "");
node.InnerText = "This is sytem comments";
XmlNode dummy = doc.InsertBefore(doc.GetElementsByTagName("Main", "")[0], node);
doc.Save("filePath");
}
thank you
Tamer KhalilRank #2,182 · 647 reputationPosted Sep 19, 2009, 7:38 PM EDT
XmlNode node = doc.CreateNode(XmlNodeType.Comment, "Comments", "");And this part is not has a problem
XmlNode dummy = doc.InsertBefore(doc.GetElementsByTagName("Main","")[0], node);Roei BarRank #1,271 · 1.4K reputationPosted Sep 20, 2009, 4:09 AM EDT
here is a sample code:
public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("" + "Pride And Prejudice" + ""); //Create a comment. XmlComment newComment; newComment = doc.CreateComment("Sample XML document"); //Add the new node to the document. XmlElement root = doc.DocumentElement; doc.InsertBefore(newComment, root); Console.WriteLine("Display the modified XML..."); doc.Save(Console.Out); }