How to find XmlComment?

  • Thread starter Thread starter Author #1
  • Start date Start date
A

Author #1

I have done a lot of search online, but could not find much helpful
information about this.

Suppose that in my XML file, I have come comments (not the type of
three-wack documentation comments) like this:

<!-- My sample comments go here -->

I have been wondering if there is any API in the .net framework that
would let me easily find such comments. I know that I can find them
through Regex, but I am interested in potential APIs in the System.Xml
or System.Xml.Linq namespaces.

Any idea? Thank you.
 
Author said:
I have done a lot of search online, but could not find much helpful
information about this.

Suppose that in my XML file, I have come comments (not the type of
three-wack documentation comments) like this:

<!-- My sample comments go here -->

I have been wondering if there is any API in the .net framework that
would let me easily find such comments. I know that I can find them
through Regex, but I am interested in potential APIs in the System.Xml
or System.Xml.Linq namespaces.

Any idea? Thank you.

Well, the XmlNodeType enumeration includes a value for "Comment", so it
seems reasonable to believe that if you read through the XML document
using the XmlReader class, you'd be able to find the XML comments.

There is also an XmlComment class, as part of the System.Xml DOM API.
So presumably you could also find comments by loading the entire XML
document as an XmlDocument object, and then searching through the nodes
for comments.

Even the System.Xml.Linq namespace has an XComment object type. So it
seems that DOM may also include comments.

I admit, I haven't tried it myself, and it's possible that all three of
those are output-only. But I would think that at least one of those
approaches would work (if there's any difficulty, I'd expect the
XmlReader to work better than the DOM APIs...but in reality, I think any
has the potential to work).

Those are pretty much the three main ways to process XML in .NET. So if
you don't find what you're looking for there, you may be stuck with
Regex or similar.

Pete
 
Author said:
I have done a lot of search online, but could not find much helpful
information about this.

Suppose that in my XML file, I have come comments (not the type of
three-wack documentation comments) like this:

<!-- My sample comments go here -->

I have been wondering if there is any API in the .net framework that
would let me easily find such comments. I know that I can find them
through Regex, but I am interested in potential APIs in the System.Xml
or System.Xml.Linq namespaces.

You can use XPath e.g.

XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
foreach (XmlComment comment in doc.SelectNodes("//comment()"))
{
Console.WriteLine(comment.Value);
}

Or you can use LINQ

XDocument doc = XDocument.Load("file.xml");
foreach (XComment comment in
doc.DescendantNodes().OfType<XComment>())
{
Console.WriteLine(comment.Value);
}


And of course XmlReader also exposes comment nodes.
 
I have been wondering if there is any API in the .net framework that
would let me easily find such comments. I know that I can find them
through Regex, but I am interested in potential APIs in the System.Xml
or System.Xml.Linq namespaces.

How are you searching for these comments?

If you are searching by parsing the file with an XML object, you can use
XPath, as Martin has stated. The LINQ to XML libs, as both Peter and
Martin have stated, works as well.

If you are walking the file with a reader, you can use Regex to find the
comments. This is not the most efficient method if you are actually
using the XML file, but is quite useful if you are merely searching a
group of files (esp. if some might not be XML).

The point here is the objective, and not the objective combined with the
solution you already have in mind, will alter the method you ultimately
use.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
How are you searching for these comments?

If you are searching by parsing the file with an XML object, you can use
XPath, as Martin has stated. The LINQ to XML libs, as both Peter and
Martin have stated, works as well.

If you are walking the file with a reader, you can use Regex to find the
comments. This is not the most efficient method if you are actually
using the XML file, but is quite useful if you are merely searching a
group of files (esp. if some might not be XML).

The point here is the objective, and not the objective combined with the
solution you already have in mind, will alter the method you ultimately
use.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog:http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************

Thk u all for your hint. I chk'd msdn and found like u guys have
hinted or said that the XmlNodeReader or the XmlTextReader class
exposes the NodeType property. So that makes this task pretty easy.
Been busy and haven't got a chnce to chk back until now. Typed up
thru my cell. Thx 4 understdng.
 
Back
Top