下面的方法是查询 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里写吧,真的好简单
本文介绍了一种使用C# LINQ to XML查询特定类型节点的方法。通过创建一个完整的XDocument实例并利用XElement来保存第一个BookParticipant元素,演示了如何遍历并获取所需的元素节点。
749

被折叠的 条评论
为什么被折叠?



