简体   繁体   English

C#Linq XML Query存在多个同名元素

[英]C# Linq XML Query where multiple elements of same name exist

Linq newbie here. 林克新手在这里。 Did the searching, and couldn't find an exact question; 搜索了,找不到确切的问题; tried to work from other answers that were similar , but still couldn't get it. 试图从其他类似的答案工作,但仍然无法得到它。

Having trouble returning all instances of a particular element name. 无法返回特定元素名称的所有实例。 I can get one item returned, just not them all. 我可以退回一件商品,而不是全部。

Here is the XML: 这是XML:

<?xml version="1.0"?>
<printerlist>
  <list type="aff">
    <printserver>print-server1</printserver>
    <printserver>print-server2</printserver>
    <printserver>print-server3</printserver>
  </list>
  <list type="lff">
    <printserver>print-sever4</printserver>
    <additionalprinters>
      <printer>
        <fullname>\\serverb\bbb</fullname>
      </printer>
      <printer>
        <fullname>\\serverc\aaa</fullname>
      </printer>
    </additionalprinters>
  </list>
</printerlist>

And here is the code to try and get the list: 以下是尝试获取列表的代码:

var query = from c in xml.Root.Descendants("list")
    where (string)c.Attribute("type") == "aff"
    select c.Element("printserver");

foreach (string name in query)
{
    Console.WriteLine("Server Name: {0}", name);
}

This only produces the first element of printserver: print-server1 How can I get the foreach to list all 3 servers that are in the aff list? 这只产生了printserver的第一个元素: print-server1如何让foreach列出关联列表中的所有3个服务器?

Thanks 谢谢

You need to use Elements("printserver") instead of Element("printserver") like so : 您需要使用Elements("printserver")而不是Element("printserver")如下所示:

var query = (from c in doc.Root.Descendants("list").Elements("printserver")
             where (string)c.Parent.Attribute("type") == "aff"
             select c);

or using SelectMany() like this : 或者像这样使用SelectMany()

var query = (from c in doc.Root.Descendants("list")
             where (string)c.Attribute("type") == "aff"
             select c).SelectMany(c => c.Elements("printserver"));

or if you are sure only one element match the where clause, you can use First() like this : 或者如果您确定只有一个元素与where子句匹配,则可以像这样使用First()

var query = (from c in doc.Root.Descendants("list")
             where (string)c.Attribute("type") == "aff"
             select c).First().Elements("printserver"));

How about something like this: 这样的事情怎么样:

List<XElement> elements = doc.Root.Descendants("printserver")
                                 .Where(e => e.Parent.Name == "list" && e.Parent.Attribute("type").Value == "aff")
                                 .ToList();

Then, if you want to loop through the elements: 然后,如果你想遍历元素:

foreach (XElement e in elements)
{
    Console.WriteLine("Server Name : {0}", e.Value);
}

If you just want the string-values of each printserver, use a .Select at the end: 如果您只想要每个printserver的字符串值,请在末尾使用.Select:

List<string> elements = doc.Root.Descendants("printserver")
                                .Where(e => e.Parent.Name == "list" && e.Parent.Attribute("type").Value == "aff")
                                .Select(p => p.Value)
                                .ToList();

Above solution allows the use of your current foreach-loop. 以上解决方案允许使用您当前的foreach循环。

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

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