?
Solved

How do I read an XML comment using C# 3.5

Posted on 2009-04-24
6
Medium Priority
?
918 Views
Last Modified: 2013-11-11
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
Comment
Question by:trey_carroll
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24226907
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
 
LVL 39

Expert Comment

by:abel
ID: 24226927
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
 

Author Comment

by:trey_carroll
ID: 24226929
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24227091
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
 
LVL 39

Expert Comment

by:abel
ID: 24227100
> The code was provided only as a guide.
ah, ok, thanks for telling ;-)

see above for a working example.
0
 

Author Closing Comment

by:trey_carroll
ID: 31574295
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question