background preloader

Xml to LINQ

Facebook Twitter

Linq to XML - Adding,Updating and Deleting data - Maor. 13 באוקטובר 2007 The previous post about Linq to XML introduced how to query XML data using LINQ.

Linq to XML - Adding,Updating and Deleting data - Maor

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. First, lets create a sample XML document: 1: XElement book = new XElement("Books", new XElement("Book", 2: new XAttribute("publisher", "O'Reilly Media, Inc 3: new XAttribute("price", "40$"), 4: new XElement("title", "Learning WCF: A Hands-on Guide"), 5: new XElement("authors", new XElement("author", "Michele Bustamante")))); 7: book.Save("Books.xml"); Adding data to the XML document 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.

The following adds a new book: Tip of the Day #93 – Reading XML with Silverlight - Silverlight Tips of the Day - Blog by Mike Snow. Skip to main content Silverlight Developer Center Sign in United States (English) © 2014 Microsoft.

Tip of the Day #93 – Reading XML with Silverlight - Silverlight Tips of the Day - Blog by Mike Snow

All rights reserved. How to: Create Dynamic XAML with LINQ to XML. To configure a Silverlight Visual Studio project to run this example In Solution Explorer, add assembly reference to the System.Xml.Linq.dll.

How to: Create Dynamic XAML with LINQ to XML

Modify your page.xaml file so it has the following content:<UserControl x:Class="SilverlightApplication1.Page" xmlns=" xmlns:x=" Width="400" Height="300"><Canvas x:Name="LayoutRoot" Background="White"></Canvas></UserControl> In the page.xaml.cs (page.xaml.vb in Visual Basic) source file for your application, add the following using statements (Imports in Visual Basic): using System.Windows.Markup; using System.Xml.Resolvers; using System.Xml; using System.Xml.Linq; using System.IO; using System.Text;

How to: Save to and Load from Isolated Storage with LINQ to XML. To configure a Silverlight Visual Studio project to run this example Modify your page.xaml file so that it includes the following TextBlock element:<TextBlock x:Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/> In the page.xaml.cs (page.xaml.vb in Visual Basic) source file for your application, add the following using statements (Imports in Visual Basic): using System.IO; using System.Xml.Linq; using System.IO.IsolatedStorage; The following example obtains the isolated storage for the user.

How to: Save to and Load from Isolated Storage with LINQ to XML

It then saves the file to an application's isolated storage and later loads the file from an isolated storage. How to: Parse XML with XmlReader. Working with XML Data. In other cases, the service developer defines a custom schema for use by the service and client, sometimes referred to as Plain Old XML (POX).

Working with XML Data

<User IsMember="true"><Name>John</Name><Age>24</Age></User> Here are three techniques available in Silverlight version 4 to parse this response. Using XmlReader XmlReader responseReader = XmlReader.Create(responseStream); responseReader.Read(); bool isMember = Boolean.Parse(responseReader.GetAttribute("IsMember")); responseReader.Read(); responseReader.ReadStartElement("Name"); string name = responseReader.ReadContentAsString(); responseReader.ReadEndElement(); responseReader.ReadStartElement("Age"); int age = responseReader.ReadContentAsInt(); responseReader.ReadEndElement(); responseReader.ReadEndElement(); For more information, see How to: Parse XML with XmlReader.

Using XLINQ This code uses the XLINQ. For more information, see Processing XML Data with LINQ to XML.