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>
4
  • 2
    It would be nice to see content of joker.xml Commented Dec 9, 2013 at 22:02
  • Possible duplicate of Reading XML comments in C# Commented Dec 9, 2013 at 22:04
  • not duplication since I want to do it in Xdocuemnt Commented Dec 9, 2013 at 22:08
  • By "comment" you mean you want to read the "NAME" attribute ? I'm not seeing anything called "comment" in the XML structure provided. Commented Dec 9, 2013 at 22:31

3 Answers 3

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 

            }

        }
Sign up to request clarification or add additional context in comments.

8 Comments

You shouldn't ALWAYS use nodes. In this case however you should.
sorry I will update but in this case it's the best way to do it
Would that return server comments? That's just dump of all comments in file
@Julie node will have the entire xml file. and when it check whether node is Xcomment it says it's not
@JohnRyann my code work but it's more tricky the code of lazyberezovsky
|
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"
}

5 Comments

no descendans will return an xelement which is different from a node
@JulieShannon take a look on OP code. He wants to take comments for servers. Servers are elements.
ok but doing like this you are filtering all elements did you understand the difference between node and element ?
@JulieShannon looks like you don't understand how my code works
ok after you have updated your code now it's working I will upvote sorry I haven't see your update
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
}

4 Comments

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
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.
A solution using XDocument is specifically asked for.
@Kevin - thanks. I was giving other alternatives. But I understand your comment and stand corrected :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.