2010-07-05 68 views

回答

2

你可以在<friends>元素中的所有元素<user>从像XML文档这个:

var url = "http://www.dreamincode.net/forums/xml.php?showuser=335389"; 
var doc = XDocument.Load(url); 
var friends = doc.Element("ipb").Element("profile") 
       .Element("friends").Elements("user"); 

// Or if you don't want to specify the whole path and you know that 
// there is only a single element named <friends>: 
var friends = doc.Descendant("friends").Elements("user"); 

然后你可以使用LINQ来处理集合。例如,要创建一个所有frineds的名称的IEnumerable<string>,你可以写:

var names = from fr in friends 
      select fr.Element("name").Value; 

如果你需要的唯一的ID,你可以以类似的方式读取<id>元素(如果它是一个整数,你可以也使用Int32.Parse解析它,例如...)

相关问题