简体   繁体   English

无法使用LINQ to XML读取XML注释

[英]Cannot read XML comments using LINQ to XML

I cannot retrieve the XML node comments. 我无法检索XML节点注释。 Is there a different way to retrieve it? 是否有其他检索方法? .Value did not work either. .Value也不起作用。 I could not find anything here in StackOverflow. 我在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? 有任何想法吗?

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>()),
        });

I found the solution in this link: https://msdn.microsoft.com/en-us/library/bb675167.aspx So the code ended up looking like this: 我在以下链接中找到了解决方案: https : //msdn.microsoft.com/zh-cn/library/bb675167.aspx因此,代码最终看起来像这样:

    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()
                                                                )
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM