首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个相同元素的LINQ-To-XML查询

多个相同元素的LINQ-To-XML查询
EN

Stack Overflow用户
提问于 2014-09-09 19:41:26
回答 2查看 51关注 0票数 0

我对LINQ和XML相当陌生,并试图使用现有的500 k行XML文件,该文件的结构与下面的XML相同。我已经知道了如何测试多个空XElements,但完全停留在如何搜索多个相同的XElements上。

如何让LINQ返回对google有效的联系人?

谢谢你们都是先进的。

代码语言:javascript
复制
void Main()
{
XDocument AddressBook = CreateAddressBookXML();

var query =
        from contact in AddressBook.Descendants("Contact")
        let companyelement = contact.Element("Company") 
        where companyelement != null
        let companyname    = companyelement.Descendants("CompanyName")
        where companyname != null && companyname == "Google"
        select contact;


Console.Write(query);

}


public XDocument CreateAddressBookXML() {
    XDocument result =
      new XDocument(
        new XComment("My phone book"),
        new XElement("phoneBook",
          new XComment("My friends"),
          new XElement("Contact",
            new XAttribute("name", "Ralph"),
            new XElement("homephone", "555-234-4567"),
            new XElement("cellphone", "555-345-75656"),
            new XElement("Company",
                new XElement("CompanyName","Ralphs Web Design"),
                new XElement("CompanyName","Google")
            )
        ),
          new XElement("Contact",
            new XAttribute("name", "Dave"),
            new XElement("homephone", "555-756-9454"),
            new XElement("cellphone", "555-762-1546"),
            new XElement("Company",
                new XElement("CompanyName","Google")
            )
        ),
          new XComment("My family"),
          new XElement("Contact",
            new XAttribute("name", "Julia"),
            new XElement("homephone", "555-578-1053"),
            new XElement("cellphone", "")
        ),
          new XComment("My team"),
          new XElement("Contact",
            new XAttribute("name", "Robert"),
            new XElement("homephone", "555-565-1653"),
            new XElement("cellphone", "555-456-2567"),
            new XElement("Company",
                new XElement("CompanyName","Yahoo")
                )
        )
    )
  );

    return result;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-09 20:33:35

代码语言:javascript
复制
var query = from contacts in CreateAddressBookXML().Root.Descendants("Contact")
            where contacts.Element("Company") != null &&
                  contacts.Element("Company").Elements("CompanyName").
            FirstOrDefault(c => c.Value == "Google") != null
            select contacts;
票数 0
EN

Stack Overflow用户

发布于 2014-09-09 21:32:35

我通常更喜欢混合一些XPath来编写这些查询,它比LINQ更简洁。

代码语言:javascript
复制
var query =
    from contact in doc.XPathSelectElements("/phoneBook/Contact")
    where contact.XPathSelectElements("Company/CompanyName[.='Google']").Any()
    select contact;

否则,使用LINQ:

代码语言:javascript
复制
var query =
    from contact in doc.Elements("phoneBook").Elements("Contact")
    where contact.Elements("Company").Elements("CompanyName")
        .Any(c => (string)c == "Google")
    select contact;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25752371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档