I have got some XML files that contain comments above the nodes. When I am reading the file in, as part of the process I would like to get the comment out as well. I know you can write a comment to the file using XmlComment, but not sure how to read them back out.
My XML looks similar to this:
<Objects>
<!--Comment about node-->
<GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362>
<info job="SAVE" person="Joe" />
<info job="SAVE" person="Sally" />
</GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362>
<!--Another Comment about node-->
<GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112>
<info job="SAVE" person="John" />
<info job="SAVE" person="Julie" />
</GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112>
Try this:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = false;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}
To read comments:
XmlReader xmlRdr = XmlReader.Create("Test.XML");
// Parse the file
while (xmlRdr.Read())
{
switch (xmlRdr.NodeType)
{
case XmlNodeType.Element:
// You may need to capture the last element to provide a context
// for any comments you come across... so copy xmlRdr.Name, etc.
break;
case XmlNodeType.Comment:
// Do something with xmlRdr.value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With