Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to add comments in an existing xml document

Tags:

c#

I need to add comments in an existing xml document.a sample xml is shown below i need to write code in c#. XML serialization was used to generate this xml any help would be great... thanks in advance

<?xml version="1.0" encoding="utf-8"?>
<Person>
<Name>Job</Name>
<Address>10dcalp</Address>
<Age>12</Age>
</Person>
like image 575
user1522673 Avatar asked Jul 13 '12 06:07

user1522673


People also ask

How do you add comments to an XML file?

Syntax. A comment starts with <! -- and ends with -->. You can add textual notes as comments between the characters.

Does XML allow comments?

Allows notes and other human readable comments to be included within an XML file. XML Parsers should ignore XML comments. Some constrains govern where comments may be placed with an XML file.

How do I add comments to XML in Visual Studio?

To insert XML comments for a code element Do one of the following: Type /// in C#, or ''' in Visual Basic. From the Edit menu, choose IntelliSense > Insert Comment. From the right-click or context menu on or just above the code element, choose Snippet > Insert Comment.

What are the limitations of placing a comment in an XML document?

Comments may not appear before the XML Declaration. Comments must not appear within an Element Tag. Comments must not appear within Attribute values. Comments within CDATA are not processed.


1 Answers

Try it like this:

        string input = @"<?xml version=""1.0"" encoding=""utf-8""?><Person><Name>Job</Name><Address>10dcalp</Address><Age>12</Age></Person>";
        XDocument doc = XDocument.Parse(input);
        XElement age = doc.Root.Element("Age");
        XComment comm = new XComment("This is comment before Age");
        age.AddBeforeSelf(comm);

This code gets the document, finds the element named "Age" which is expected to be under the root element ("Person") and adds comment before it.

like image 114
Ivan Golović Avatar answered Oct 26 '22 07:10

Ivan Golović