Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Linq to XML - Adding,Updating and Deleting data<br />The previous post about Linq to XML introduced how to query XML data ...
새 Microsoft office word 문서
새 Microsoft office word 문서
Upcoming SlideShare
Loading in …5
×

새 Microsoft office word 문서

150 views

Published on

  • Be the first to comment

  • Be the first to like this

새 Microsoft office word 문서

  1. 1. Linq to XML - Adding,Updating and Deleting data<br />The previous post about Linq to XML introduced how to query XML data using LINQ. LINQ allows us to not only query XML in a truly unique way, but also create XML documents in a very expressive manner. This post will talk about other operations on the XML: adding, updating and deleting data.<br />First, lets create a sample XML document:<br /> 1: XElement book = new XElement("Books", new XElement("Book",<br /> 2: new XAttribute("publisher", "O'Reilly Media, Inc."),<br /> 3: new XAttribute("price", "40$"),<br /> 4: new XElement("title", "Learning WCF: A Hands-on Guide"),<br /> 5: new XElement("authors", new XElement("author", "Michele Bustamante"))));<br /> 6:  <br /> 7: book.Save("Books.xml");<br />Adding data to the XML document<br />Adding XML to the existing XML document is very simple, we need only construct our XML using a mixture of XElement and XAttribute types (there are other ways also...) and then add them to the document. <br />The following adds a new book:<br /> 1: XElement doc = XElement.Load("Books.xml");<br /> 2: XElement newBook = new XElement("Book",<br /> 3: new XAttribute("publisher", "Microsoft Press"),<br /> 4: new XAttribute("price", "45$"),<br /> 5: new XElement("title", "Introducing Microsoft LINQ"),<br /> 6: new XElement("authors", new XElement("author", "Paolo Pialorsi"), <br /> 7: new XElement("author", "Marco Russo")));<br /> 8:  <br /> 9: doc.Add(newBook);<br /> 10: doc.Save("Books.xml");<br />We must save the xml with the save method, because in LINQ to XML, no changes are made to the loaded XML document until that document is saved.<br />The XML document now is:<br /> 1: <?xml version="1.0" encoding="utf-8"?><br /> 2: <Books><br /> 3: <Book publisher="O'Reilly Media, Inc." price="40$"><br /> 4: <title>Learning WCF: A Hands-on Guide</title><br /> 5: <authors><br /> 6: <author>Michele Bustamante</author><br /> 7: </authors><br /> 8: </Book><br /> 9: <Book publisher="Microsoft Press" price="45$"><br /> 10: <title>Introducing Microsoft LINQ</title><br /> 11: <authors><br /> 12: <author>Paolo Pialorsi</author><br /> 13: <author>Marco Russo</author><br /> 14: </authors><br /> 15: </Book><br /> 16: </Books><br />Updating data<br />Updating XML data is also very simple; Just pick the element/attribute you wish to update and then set its new value. <br /> 1: XElement doc = XElement.Load("Books.xml");<br /> 2:  <br /> 3: //obtain a single book<br /> 4: IEnumerable<XElement> singleBook = (from b in doc.Elements(<br /> 5: "Book")<br /> 6: where ((string)b.Element(<br /> 7: "title")).Equals("Introducing Microsoft LINQ")<br /> 8: select b);<br /> 9:  <br /> 10: //update book, should only be 1<br /> 11: foreach (XElement xe in singleBook)<br /> 12: {<br /> 13: xe.SetAttributeValue("price", "39$");<br /> 14: <br /> 15: //use the ReplaceContent method to do the replacement for all attribures<br /> 16: //this will remove all other attributes and save only the price attribute<br /> 17: xe.ReplaceAttributes(new XAttribute("price", "32$"));<br /> 18: }<br /> 19:  <br /> 20: doc.Save("Books.xml");<br />Deleting data<br />We simply have to get the object we want to delete and then delete it using the Remove() method. <br /> 1: XElement doc = XElement.Load("Books.xml");<br /> 2:  <br /> 3: //obtain the first Book<br /> 4: IEnumerable<XElement> firstBook = (from b in doc.Elements(<br /> 5: "Book")<br /> 6: select b).Take(1);<br /> 7:  <br /> 8: //delete book price<br /> 9: foreach (XElement xe in firstBook)<br /> 10: {<br /> 11: xe.Attribute("price").Remove();<br /> 12: }<br /> 13:  <br /> 14: doc.Save("Books.xml");<br />Other way: we pass a lambda expression in as an argument to the Where extension method. <br />As you can see, Xlinq is really simple and great way to work with XML. <br />Enjoy!<br />Technorati <br />

×