Home >
Long have developers loathed iterating through text data, parsing end of line characters and field separators. Fixed length files and delimited files were the bane of a developer's existence.
Manager: We need you to pull in some data from our warehouse to our system. Integrate the 2.
Developer: Wait ... this data is delimited text files. Is there a schema? How do I know what fields are what? Is the format always the same?
Manager: Thanks, I need it by Friday. Good luck!
Developer: Wait!!!
Maybe I exaggerate a little but you get the idea. Its just not fun to parse data. Even when you know the format, as with XML files, tools such as an XmlReader which can iterate through the data leave a lot to be desired. Searching XML nodes for values, especially since many nodes may not even be required, can be a real pain. Enter LINQ to XML. LINQ to XML makes parsing XML much easier than iterating through rows. So instead of looping through rows of data from a service, such as Amazon, to get the results of a Amazon product search, a query can be written using LINQ to XML to pull out what you need. The format is pretty simple as it looks a lot like SQL. For example:
This LINQ to XML query accepts an XML string that contains the results from a search using Amazon's web services. The format is pretty straightforward, even though I used some more involved constructs in the query. This structure beats iterating through results. Its not strongly typed as it takes the name of the XML nodes and attributes, but the query structure basically asks it to pull out all of the products that have an ISBN gather their information, including the link to its image, and then pull them into a new book class instance. At that point the list of book classes can be bound to the UI and displayed in a snap.
When Silverlight 2 applications communicate with web services, many of them return data as XML or JSON (more on this in another post). While XmlReader can be used to iterate through the data and parse out what is needed, LINQ to XML can really make this much simpler to write and maintain.
Manager: We need you to pull in some data from our warehouse to our system. Integrate the 2.
Developer: Wait ... this data is delimited text files. Is there a schema? How do I know what fields are what? Is the format always the same?
Manager: Thanks, I need it by Friday. Good luck!
Developer: Wait!!!
Maybe I exaggerate a little but you get the idea. Its just not fun to parse data. Even when you know the format, as with XML files, tools such as an XmlReader which can iterate through the data leave a lot to be desired. Searching XML nodes for values, especially since many nodes may not even be required, can be a real pain. Enter LINQ to XML. LINQ to XML makes parsing XML much easier than iterating through rows. So instead of looping through rows of data from a service, such as Amazon, to get the results of a Amazon product search, a query can be written using LINQ to XML to pull out what you need. The format is pretty simple as it looks a lot like SQL. For example:
var bookQuery = from book in bookXml.Descendants(ns + "Item")
let attributes = book.Element(ns + "ItemAttributes")
let imageSource =
(book.Elements(ns + "SmallImage").Any()
? book.Element(ns + "SmallImage").Element(ns + "URL").Value
: string.Empty)
let year = Int32.Parse((attributes.Element(ns + "PublicationDate").Value).Substring(0, 4))
where attributes.Elements(ns + "ISBN").Any()
orderby attributes.Element(ns + "Title").Value ascending
select new Book
{
AmazonASIN = ((string)book.Element(ns + "ASIN")).Trim(),
ISBN = attributes.Element(ns + "ISBN").Value,
Title = attributes.Element(ns + "Title").Value,
Publisher = attributes.Element(ns + "Publisher").Value,
Year = year,
HrefLink = new Uri(book.Element(ns + "DetailPageURL").Value),
Authors = (
from author in book.Descendants(ns + "Author")
select author.Value
).ToList(),
ImageSource = imageSource
};
This LINQ to XML query accepts an XML string that contains the results from a search using Amazon's web services. The format is pretty straightforward, even though I used some more involved constructs in the query. This structure beats iterating through results. Its not strongly typed as it takes the name of the XML nodes and attributes, but the query structure basically asks it to pull out all of the products that have an ISBN gather their information, including the link to its image, and then pull them into a new book class instance. At that point the list of book classes can be bound to the UI and displayed in a snap.
When Silverlight 2 applications communicate with web services, many of them return data as XML or JSON (more on this in another post). While XmlReader can be used to iterate through the data and parse out what is needed, LINQ to XML can really make this much simpler to write and maintain.




Facebook Application Development
This LINQ to XML query accepts an XML string that contains the results from a search using Amazon's web services. Very interesting article! many useful informations :)