LINQ To XML:修改结点内容

XDocument xDocument = new XDocument( new XElement("BookParticipants", firstParticipant = new XElement("BookParticipant", new XComment("This is a new author."), new XAttribute("type", "Author"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")))); Console.WriteLine("Before updating nodes:"); Console.WriteLine(xDocument); // Now, lets update an element, a comment, and a text node. firstParticipant.Element("FirstName").Value = "Joey"; firstParticipant.Nodes().OfType<XComment>().Single().Value = "Author of Pro LINQ: Language Integrated Query in C# 2008."; ((XElement)firstParticipant.Element("FirstName").NextNode) .Nodes().OfType<XText>().Single().Value = "Rattz, Jr."; Console.WriteLine("After updating nodes:"); Console.WriteLine(xDocument);  

 

输出

 

Before updating nodes: <BookParticipants> <BookParticipant type="Author"> <!--This is a new author.--> <FirstName>Joe</FirstName> <LastName>Rattz</LastName> </BookParticipant> </BookParticipants> After updating nodes: <BookParticipants> <BookParticipant type="Author"> <!--Author of Pro LINQ: Language Integrated Query in C# 2008.--> <FirstName>Joey</FirstName> <LastName>Rattz, Jr.</LastName> </BookParticipant> </BookParticipants>  

你可能感兴趣的:(LINQ To XML:修改结点内容)