Lord-David
Lord-David

Reputation: 545

How to comment an XMLElement using C#?

My application reads values from an xml file which I write everytime when I execute the application.

This is how I made comments of my lines:

XmlComment DirCom = doc.CreateComment("Comment")
XmlElementName.AppendChild(DirCom);

And It works fine, But now I need to comment the XML element, And the above doesn't work. My final result should be like:

<!--<name>David</name>-->

using C# and the XML document library.

Upvotes: 3

Views: 1897

Answers (1)

MathB.Fr
MathB.Fr

Reputation: 292

To comment an xml node I would do it like this :

XmlComment DirCom = doc.CreateComment(XmlElementName.OuterXml);
doc.InsertAfter(DirCom, XmlElementName);    
doc.RemoveChild(XmlElementName)

Upvotes: 5

Related Questions