繁体   English   中英

根据父元素的值获取xml的子元素

[英]Get child elements of xml based on parent element's value

我有一个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>

我正在尝试将每个版本的信息输出到我的网页。 到目前为止,我能够获得版本号,但无法将注释与版本号相关联。 这就是我所拥有的:

var xml = Client.GetReleaseNotes();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

var versions = Client.GetUniqueNames(doc.SelectNodes("//Version"));
versions.Reverse();

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

如下所示,我使用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循环并尝试抓取每个版本的子元素。 通过这种方式,我可以将笔记与其版本相关联。 我读到你可以使用SelectNodes方法根据特定条件(如父节点的值)获取子节点。 这就是促使我创建行var notes = Client.GetVersionNotes(doc.SelectNodes("//version[@value='" + version + "']/note"));

但是我仍然无法得到笔记。 我究竟做错了什么? 有人可以指点我正确的方向吗?

请注意,XPath中的元素和属性名称区分大小写。 此外,XPath中的属性名称错误(应该是@Name而不是@value )。 尝试这种方式来获取相应的Note元素:

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

另一个选择是使用LINQ to 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);

这给你这个查找:

抬头

然后你可以这样做:

var notes = lookup["2.1"].ToList();

这会给你:

结果

暂无
暂无

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

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