2

How to read xml comment when using XDocument?

XDocument doc = XDocument.Load("joker.xml");
 foreach (XElement element in doc.Descendants("server"))
            {
//I use this to read server tag...
}



<?xml version="1.0" encoding="ISO-8859-1"?>
<ho>
    <!-- For X use only -->
    <servers>
        <server NAME="xx" ></server>

    <!-- Dummy servers  -->
        <server NAME="xx" ></server>
        <server NAME="xx" ></server>
    </servers>
</ho>
John Ryann
  • 2,283
  • 11
  • 43
  • 60

3 Answers3

5

The Node object is the primary data type for the entire DOM.

A node can be an element node, an attribute node, a text node, or any other of the node types explained in the "Node types" chapter.

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

     XDocument xdoc = XDocument.Load("");
       foreach (var node in xdoc.Descendants("servers").Nodes())
        {

            if (node is XComment)
            {
                //THEN  READ YOUR COMMENT 

            }

        }
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
4

Check node type when reading xml. If it's XComment then you are reading comment. E.g. in your case previous node of server element will be comment:

foreach(var s in doc.Descendants("server"))
{
    var comment = s.PreviousNode as XComment;
    if (comment != null)
        Console.WriteLine(comment.Value); // outputs "Dummy servers"
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

you will have to use the XmlReader.Create method and read it and switch between the nodes to indicate which node it is current reading Dont be fooled by the Create method... it reads the xml file in question but creates an instance of the XmlReader object:

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.create(v=vs.110).aspx

XmlReader xmlRdr = XmlReader.Create("Joker.XML");
// Parse the file
while (xmlRdr.Read())
{
    switch (xmlRdr.NodeType)
    {
        case XmlNodeType.Element:
            // Current node is an Xml Element
            break;
        case XmlNodeType.Comment:
            // This is a comment so do something with xmlRdr.value

... and so on

PART 2 - for those who want to use LINQ (not that it makes a difference really)...

XDocument xml = XDocument.Load("joker.xml");
var commentNodes = from n in xml.Descendants("server")
                where n.NodeType == XmlNodeType.Comment
                select n;

foreach(XNode node in commentNodes)
{ 
  // now you are iterating over the comments it has found
}
Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
  • 1
    no this is not an answer with Xdocument you have the support of Linq to xml he want a solution to his code not a new way to do it – BRAHIM Kamel Dec 09 '13 at 22:04
  • Sure but what difference does it make? LINQ just really makes it a little easier to read things. it doesnt change functionality. its another way of expressing things :). Furthermore the original question has not shown any usage on LINQ anyway. – Ahmed ilyas Dec 09 '13 at 22:05
  • A solution using XDocument is specifically asked for. – Kevin Dec 09 '13 at 22:17
  • @Kevin - thanks. I was giving other alternatives. But I understand your comment and stand corrected :) – Ahmed ilyas Dec 09 '13 at 22:18