1

I cannot retrieve the XML node comments. Is there a different way to retrieve it? .Value did not work either. I could not find anything here in StackOverflow. This is what I am doing:

<?xml version="1.0" encoding="utf-8"?>
<components>
    <component name="AAA">
        <sample>
            <!-- blah blah blah --> 
        </sample>
        <data>
            <!-- blah blah blah -->             
        </data>
    </component>

    <component name="BBB">
        <sample>
            <!-- blah blah blah --> 
        </sample>
        <data>
            <!-- blah blah blah -->             
        </data>     
    </component>    
</components>

public class Component
{
    public string Name { get; set; }
    public string Sample { get; set; }
    public string Data { get; set; }
}

    XDocument doc = XDocument.Load(xmlFileName);

    Component components = doc.Descendants("component")
        .Select(x => new Component
        {
            Name = (string)x.Attribute("name"),
            Sample = (string)x.Element("sample"), //cannot read value
            Data = (string)x.Element("data") //cannot read value
        });

Any ideas?

1
  • Hint: comments are not elements Commented Mar 31, 2015 at 23:26

2 Answers 2

3

Try this:

var components =
    doc
        .Descendants("component")
        .Select(x => new Component()
        {
            Name = (string)x.Attribute("name"),
            Sample = String.Join("", x.Element("sample").Nodes().OfType<XComment>()),
            Data = String.Join("", x.Element("data").Nodes().OfType<XComment>()),
        });
Sign up to request clarification or add additional context in comments.

4 Comments

Now I am getting: Error 2 Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XComment>' to 'string[]'
You may not have a reference you need or you're using an earlier framework - it worked for me as it is.
yep that worked in .Net4.0 but not 3.5. We cannot use 4.0 at this time..sighss I know right.
@Max - Did you try putting a .ToArray() after each of the OfType calls?
1

I found the solution in this link: https://msdn.microsoft.com/en-us/library/bb675167.aspx So the code ended up looking like this:

    var components = doc
        .Descendants("component")
        .Select(x => new Component()
        {
            Name = (string)x.Attribute("name"),
            Sample = x.Element("sample").Nodes().OfType<XComment>()
                                                                .Select(s=>s.Value)
                                                                .Aggregate(
                                                                    new StringBuilder(), 
                                                                    (s,i)=>s.Append(i),
                                                                    s => s.ToString()
                                                                ),
            Data = x.Element("data").Nodes().OfType<XComment>()
                                                                .Select(s=>s.Value)
                                                                .Aggregate(
                                                                    new StringBuilder(), 
                                                                    (s,i)=>s.Append(i),
                                                                    s => s.ToString()
                                                                )
        });

Comments

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.