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

下面的方法是查询 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里写吧,真的好简单

你可能感兴趣的:(xml,null,LINQ,reference)