Introducing LINQ with XML and Databases - XML Literals in Visual Basic
(Page 4 of 4 )
There's one final thing I'd like to mention on the topic of LINQ to XML. If you're using Visual Basic, you can drop literal XML right into your code:
Dim document As XDocument = _
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<people>
<person>
<name>Bob</name>
<age>35</age>
</person>
<person>
<name>Henry</name>
<age>43</age>
</person>
<person>
<name>Joe</name>
<age>22</age>
</person>
<person>
<name>Chuck</name>
<age>29</age>
</person>
</people>
In order to recreate the last example, you would simply wrap the query in <%= %> tags:
Dim overThirtyDocument As XDocument = _
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<people>
<%= From p In document.Descendants("person") _
Where CInt(p.Element("age")) > 30 _
Select p %>
</people>
Conclusion
LINQ is a powerful new tool in the .NET developer's toolbox. It integrates data access directly into .NET languages, providing a very readable query language that can be used on a variety of data sources. In these two articles, we covered objects, XML files and databases, but LINQ supports other data sources, too, that you may want to look into.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |