C# 基于父元素获取xml的子元素';s值

C# 基于父元素获取xml的子元素';s值,c#,asp.net,xml,C#,Asp.net,Xml,我有一个XML字符串,如下所示: <?xml version="1.0" encoding="utf-8"?> <Report> <Version Name="2.0"> <Note>The meaning of life is 42.</Note> <Note>Hitchiker's Guide to the Galaxy</Note> </Version> <Ve

我有一个XML字符串,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Report>
  <Version Name="2.0">
    <Note>The meaning of life is 42.</Note>
    <Note>Hitchiker's Guide to the Galaxy</Note>
  </Version>
  <Version Name="2.1">
    <Note>Football is awesome!</Note>
    <Note>Let's go sailing!</Note>
  </Version>
</Report>
其中
Client.GetReleaseNotes()为我提供要解析的XML字符串

public List<string> GetUniqueNames(XmlNodeList nodes)
        {
            List<string> list = new List<string>();
            foreach (XmlNode node in nodes)
            {
                var attr = node.Attributes["Name"] ?? node.Attributes["Title"];
                string name = attr.InnerText;
                if (!list.Contains(name) && name.IndexOf("sample", StringComparison.OrdinalIgnoreCase) == -1)
                    list.Add(name);
            }

            list.Sort();

            return list;
        }

public List<string> GetVersionNotes(XmlNodeList nodes)
        {
            List<string> list = new List<string>();

            foreach (XmlNode node in nodes)
            {
                list.Add(node.Value);
            }

            list.Sort();

            return list;
        }
公共列表GetUniqueNames(XmlNodeList节点) { 列表=新列表(); foreach(节点中的XmlNode节点) { var attr=节点.属性[“名称”]??节点.属性[“标题”]; 字符串名称=attr.InnerText; 如果(!list.Contains(name)&&name.IndexOf(“sample”,StringComparison.OrdinalIgnoreCase)=-1) 列表。添加(名称); } list.Sort(); 退货清单; } 公共列表GetVersionNotes(XmlNodeList节点) { 列表=新列表(); foreach(节点中的XmlNode节点) { list.Add(node.Value); } list.Sort(); 退货清单; }
正如您在下面看到的,我使用foreach循环将版本号输出到屏幕

<section id="release-notes">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">Release Notes</h2>
            </div>
        </div>
        <div class="row">
            @foreach (string version in versions)
            {
                <div class="col-md-4">
                    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                        <div class="panel panel-default">
                            <div class="panel-heading" role="tab" id="webHeading">
                                <h4 class="panel-title">
                                    <a role="button" data-toggle="collapse" data-parent="#accordion" href="#webPanel" aria-expanded="true" aria-controls="webPanel">
                                        Version @version
                                    </a>
                                </h4>
                            </div>
                            <div id="webPanel" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="webHeading">
                                <ul class="list-group">
                                    @{
                                        var notes = Client.GetVersionNotes(doc.SelectNodes("//version[@value='" + version + "']/note"));
                                    }

                                    @foreach (string note in notes)
                                    {
                                        <li class="list-group-item">@note</li>
                                    }
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</section>

发行说明
@foreach(版本中的字符串版本)
{
    @{ var notes=Client.GetVersionNotes(doc.SelectNodes(“//version[@value='”+version+']/note”); } @foreach(注释中的字符串注释) {
  • @注意
  • }
}
然后,我继续使用另一个foreach循环,并尝试获取每个版本的子元素。这样我就可以把笔记和它的版本联系起来。我了解到您可以使用SelectNodes方法根据特定条件(如父节点的值)获取子节点。这就是促使我创建行
var notes=Client.GetVersionNotes(doc.SelectNodes(“//version[@value=”“+version+”]/note”)


但是我还是拿不到笔记。我做错了什么?有人能给我指出正确的方向吗?

注意XPath中的元素和属性名称区分大小写。此外,XPath中的属性名称也错误(应该是
@name
,而不是
@value
)。尝试使用此方法获取相应的
注释
元素:

doc.SelectNodes("//Version[@Name='" + version + "']/Note")

另一种选择是使用LINQ转换XML

以下是方法:

var doc = XDocument.Parse(@"<?xml version=""1.0"" encoding=""utf - 8""?>
<Report>
  <Version Name = ""2.0"">
    <Note>The meaning of life is 42.</Note>
    <Note>Hitchiker's Guide to the Galaxy</Note>
  </Version>
  <Version Name = ""2.1"">
    <Note>Football is awesome!</Note>
    <Note>Let's go sailing!</Note>
  </Version>
</Report>");

var lookup =
    doc
        .Descendants("Note")
        .ToLookup(
            x => x.Parent.Attribute("Name").Value,
            x => x.Value);
这就给了你:


通过更改您的建议并在版本上大写“我的V”,现在可以将正确数量的项目添加到列表中,但项目为空。请尝试在GetVersionNotes()中使用node.InnerText而不是oDnode.Value
var doc = XDocument.Parse(@"<?xml version=""1.0"" encoding=""utf - 8""?>
<Report>
  <Version Name = ""2.0"">
    <Note>The meaning of life is 42.</Note>
    <Note>Hitchiker's Guide to the Galaxy</Note>
  </Version>
  <Version Name = ""2.1"">
    <Note>Football is awesome!</Note>
    <Note>Let's go sailing!</Note>
  </Version>
</Report>");

var lookup =
    doc
        .Descendants("Note")
        .ToLookup(
            x => x.Parent.Attribute("Name").Value,
            x => x.Value);
var notes = lookup["2.1"].ToList();