• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 928
  • Last Modified:

How do I read an XML comment using C# 3.5

I need to read the 1st comment in an xml file with the following format:

<root>
     <!--My comment-->
     <child1>blah</child2>
     ...
</root>

My XML is contained in an XDocument object in a C# 3.5 application.  

I just need to do something like;

//does not work
string s = xDoc.xpathSelectElement("\root\following::comment()[0]");

Thanks
<root>
     <!--My comment-->
     <child1>blah</child2>
     ...
</root>
 
//does not work
string s = xDoc.xpathSelectElement("\root\following::comment()[0]");

Open in new window

0
trey_carroll
Asked:
trey_carroll
  • 4
  • 2
1 Solution
 
abelCommented:
itneresting. But it cannot compile either. Is that really your source code? Just asking, because I would try to prevent hunting down a bug that a compiler could catch...
0
 
abelCommented:
ah, it can compile. You are just unlucky with the words! \r is carriage return, \f is formfeed. Try this:

string s = xDoc.xpathSelectElement(@"\root\following::comment()[0]");
the @-prefix will make it unnecessary for you to have to escape the backslashes. Or you need to do this, instead:

string s = xDoc.xpathSelectElement("\\root\\following::comment()[0]");
but the,.... it is still not an XPath, so you won't get much. An XPath uses forward slashes. So, in the end, it becomes this:

string s = xDoc.xpathSelectElement("/root/following::comment()[0]");
0
 
trey_carrollAuthor Commented:
The code was provided only as a guide.  The syntax is from an MSXML 2.0 example that I found on the web - provides as a "works sort of like this" guide.    I am looking for a new way since the way that I am trying does not work.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
abelCommented:
Note that it still won't work. Looking further into your code, you are using C#, so you need to be case-aware. You need at least:

string s = xDoc.XPathSelectElement("/root/following::comment()[0]");
but that still won't compile, because an XElement won't be converted to a string. I am wondering what code you had that the compiler did not complain about ;)

Anyway, having an XElement and selecting and element is not going to give you what you want, still. Because a comment simply is not an element. So it will never work. You can use XPathEvaluate instead, like so, to select the first comment you encounter and to convert it to a string:

TextReader textRdr = new StringReader("<root><!-- comment is here --><othernode /></root>");
XDocument xdoc = XDocument.Load(textRdr);
XNode commentNode = ((IEnumerable)xdoc.XPathEvaluate("/root/comment()")).Cast<XNode>().FirstOrDefault();
string sComment = commentNode.ToString();

Open in new window

0
 
abelCommented:
> The code was provided only as a guide.
ah, ok, thanks for telling ;-)

see above for a working example.
0
 
trey_carrollAuthor Commented:
Thank you so much for such an excellent solution.  You might post a followup pointing out that line4 will cause a NullPointer Exception when the comment is found.   I'm not sure if I can do this - this is my 1st week using the service.  Thanks again - this information is nearly IMPOSSIBLE to locate on the net due to the facts that every tech site includes a section about comments,  there are so many articles on VS's new XML Comments in code - it makes it impossible to filter out the vast number of false positives.   You have made my day!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now