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")))); Console.WriteLine(firstParticipant.Element("FirstName"));
输出
<FirstName>Joe</FirstName>
firstParticipant.Element("FirstName")
这里没写循环,所以只打印出第一个元素
本文介绍了一个使用XDocument创建XML文档的实例,展示了如何通过XDocument构造包含声明、类型定义、处理指令及元素的完整XML文档,并保存对特定元素的引用。
189

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



