gpt4 book ai didi

c# - 无法使用 LINQ to XML 读取 XML 注释

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:00 25 4
gpt4 key购买 nike

我无法检索 XML 节点注释。有没有不同的方法来检索它? .Value 也不起作用。我在 StackOverflow 中找不到任何东西。这就是我正在做的:

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

有什么想法吗?

最佳答案

试试这个:

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

关于c# - 无法使用 LINQ to XML 读取 XML 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29380232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com