代码之家  ›  专栏  ›  技术社区  ›  Sergio Tapia

如果有三个相同的元素,如何选择XML元素?

  •  1
  • Sergio Tapia  · 技术社区  · 14 年前

    鉴于此 XML found here .

    如何分别获取每个联系人项目?

    我试过这个:

    return doc.XPathSelectElement("/ipb/profile/contactinformation/contact[type/text() = 'LinkedIn']/value").Value;
    

    2 回复  |  直到 14 年前
        1
  •  2
  •   Gregory Higley    14 年前

    /test/contactinfo/contact[type = 'Twitter']/address

    如果不起作用,试试看

    /test/contactinfo/contact[type/text() = 'Twitter']/address

        2
  •  0
  •   dtb    14 年前
    var profile = doc.Root.Element("profile");
    
    var contactinfo = profile.Element("contactinformation");
    
    var contacts = from contact in contactinfo.Elements("contact")
                   where (string)contact.Element("title") == "Twitter"
                   select contact;
    
    var result = (string)contacts.Single().Element("value");