您的位置:首页 > 其它

Linq To XML:在结点下查询某一类型结点

2011-04-21 16:15 387 查看
下面的方法是查询 element 元素类型的结点



XElement firstParticipant; 
 
//  A full document with all the bells and whistles. 
XDocument xDocument = new XDocument( 
  new XDeclaration("1.0", "UTF-8", "yes"), 
  new XDocumentType("BookParticipants", null, "BookParticipants.dtd", null), 
  new XProcessingInstruction("BookCataloger", "out-of-print"), 
  //  Notice on the next line that we are saving off a reference to the first 
  //  BookParticipant element. 
  new XElement("BookParticipants", firstParticipant = 
    new XElement("BookParticipant", 
      new XComment("This is a new author."), 
      new XProcessingInstruction("AuthorHandler", "new"), 
      new XAttribute("type", "Author"), 
      new XElement("FirstName", "Joe"), 
      new XElement("LastName", "Rattz")), 
    new XElement("BookParticipant", 
      new XAttribute("type", "Editor"), 
      new XElement("FirstName", "Ewan"), 
      new XElement("LastName", "Buckingham")))); 
 
foreach (XNode node in firstParticipant.Nodes().OfType<XElement>()) 
{ 
  Console.WriteLine(node); 
}




输出



<FirstName>Joe</FirstName> 
<LastName>Rattz</LastName>




firstParticipant.Nodes().OfType<XElement>()



想取什么类型就在OfType里写吧,真的好简单
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: