<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fremus.co.za &#187; LINQ to XML</title>
	<atom:link href="http://www.fremus.co.za/blog/category/linq-to-xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fremus.co.za/blog</link>
	<description>Demistifying Life and Web Development</description>
	<lastBuildDate>Sun, 27 Nov 2011 15:56:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting a single XAttribute or XElement</title>
		<link>http://www.fremus.co.za/blog/2009/11/getting-a-single-xattribute-or-xelement/</link>
		<comments>http://www.fremus.co.za/blog/2009/11/getting-a-single-xattribute-or-xelement/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 13:15:43 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ to XML]]></category>
		<category><![CDATA[XAttribute]]></category>
		<category><![CDATA[XElement]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=534</guid>
		<description><![CDATA[I have come across two ways querying XElements or XAttributes: Make an object IEnumerable and loop through the results Use the Single() method to return a single object only Typically you may have a situation where you write some Linq-to-XML like this: IEnumerable attrib = from att in elemInner.Attributes() where (string)att.Name.ToString() == "sectionName" select att; [...]]]></description>
			<content:encoded><![CDATA[<p>I have come across two ways querying XElements or XAttributes:</p>
<ul>
<li>Make an object IEnumerable and loop through the results</li>
<li>Use the <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.single.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/system.linq.enumerable.single.aspx?referer=');">Single()</a> method to return a single object only</li>
</ul>
<p>Typically you may have a situation where you write some Linq-to-XML like this:</p>
<pre name="code" class="csharp">
IEnumerable<XAttribute> attrib = from att in elemInner.Attributes()
                                             where (string)att.Name.ToString() == "sectionName"
                                             select att;
</pre>
<p>To loop through the results you have to use a foreach loop like this:</p>
<pre name="code" class="csharp">

foreach (XAttribute innerAttrib in attrib)
{
     builder.AppendLine("
<h3>" + innerAttrib.Value +"</h3>

");
}
</pre>
<p>If however you just wanted a single attribute result, you could write the same code like this:</p>
<pre name="code" class="csharp">

 XAttribute attribDisplayStyle = (from att in elemInner.Attributes()
                                            where (string)att.Name.ToString() == "sectionName"
                                            select att).Single();
</pre>
<p>Then you only have a single XAttribute instance and you dont need a foreach loop:</p>
<pre name="code" class="csharp">

attribDisplayStyle.Value;
</pre>
<p>You would apply the same logic to XElement as well.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/11/getting-a-single-xattribute-or-xelement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting search results from Bing REST service and using LINQ to process results</title>
		<link>http://www.fremus.co.za/blog/2009/10/getting-search-results-from-bing-rest-service-and-using-linq-to-process-results/</link>
		<comments>http://www.fremus.co.za/blog/2009/10/getting-search-results-from-bing-rest-service-and-using-linq-to-process-results/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 15:18:38 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[Bing]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ to XML]]></category>
		<category><![CDATA[Bing API]]></category>
		<category><![CDATA[Bing REST]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=520</guid>
		<description><![CDATA[So today I started reading about the Bing API and I got myself an API key and I read through the basic instruction manual, which tells you how to get search results from the web through the Bing REST service. Its pretty straight forward, just get your own key though! But here is some sample [...]]]></description>
			<content:encoded><![CDATA[<p>So today I started reading about the Bing API and I got myself an API key and I read through the basic instruction manual, which tells you how to get search results from the web through the Bing REST service. Its pretty straight forward, just get your own key though! But here is some sample code that does the trick and uses Linq to XML to process the results:</p>
<pre name="code" class="csharp">
            XDocument document = XDocument.Load("http://api.search.live.net/xml.aspx?Appid=<YourApiKey>&#038;query=sushi&#038;sources=web");
            XElement root = document.Root;
            XNamespace web = "http://schemas.microsoft.com/LiveSearch/2008/04/XML/web";
            var searchItems = document.Descendants(web + "Results").SingleOrDefault();

            IEnumerable<XElement> testelem = from el in searchItems.Elements()
                                                 select el;
            foreach (XElement myElem in testelem)
            {
                context.Response.Write(myElem.Value + "");
            }
</pre>
<p>That is very easy!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/10/getting-search-results-from-bing-rest-service-and-using-linq-to-process-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enumerating through an XML document</title>
		<link>http://www.fremus.co.za/blog/2009/10/enumerating-through-an-xml-document/</link>
		<comments>http://www.fremus.co.za/blog/2009/10/enumerating-through-an-xml-document/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 20:16:26 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ to XML]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=504</guid>
		<description><![CDATA[I am in the process of developing some code for easily updating my CV, and one of the technologies I am using for this purpose is Linq to XML. As I have said previously on this blog, Linq to XML is an in-memory XML manipulation technology which allows you to manipulate XML as if you [...]]]></description>
			<content:encoded><![CDATA[<p>I am in the process of developing some code for easily updating my CV, and one of the technologies I am using for this purpose is Linq to XML. As I have said <a href="http://www.fremus.co.za/blog/2009/10/growing-into-learning-linq-to-xml/">previously on this blog</a>, Linq to XML is an in-memory XML manipulation technology which allows you to manipulate XML as if you would use the XML DOM. So as part of my application I want to use XML because its easy to create or get the structure needed for creating the CV. I have discussed the idea of a CV Builder previously <a href="http://www.fremus.co.za/blog/2009/09/concept-cv-updater/">here</a>. So part of this project involves reading XML and reading XML with LINQ to XML seems a rather straight forward process. The first thing you need to know about though is that the IEnumerable interface you need to use exists in the System.Collections.Generic namespace, not System.Collections. If you use only the latter you will get an error message saying:</p>
<p><em>The non-generic type &#8216;System.Collections.IEnumerable&#8217; cannot be used with type arguments</em></p>
<p>Lets show some code that reads XML:</p>
<pre name="code" class="csharp">
        XDocument document = XDocument.Load(context.Server.MapPath(@"CVTemplate.xml"));
        XElement root = document.Root;
        IEnumerable<XElement> elements = from el in root.Descendants("parentsection")
                                             select el;
        foreach (XElement elem in elements)
        {
            builder.AppendLine("Element Name:\t" + elem.Name.ToString());
            builder.AppendLine("Attributes:");
            foreach (XAttribute attrib in elem.Attributes())
            {
                builder.AppendLine("\t\t" + attrib.Value);
            }
            foreach (XElement subElem in elem.Descendants())
            {
                builder.AppendLine("\t\t" + subElem.Name.ToString() + "\n\t\t\t" + subElem.Value);
            }
        }
        context.Response.Write(builder.ToString());
</pre>
<p>In the code above I use the XDocument class to read the XML from an XML file that I specified in a web.config app setting. I then create an XElement instance by assigning it to the document.Root of the XDocument instance, because its return type is XElement. I then create an enumerable variable elements of type XElement and get the enumerable result from a LINQ query that selects the elements from the XML of the nodes parentsection. I then enumerate through the XElements from the elements variable. Inside the foreach loop I enumerate twice again, once for the attributes of the XML node and the second time for the nested XElements. I just created a StringBuilder instance to write the values as the enumeration took place, its just so that I can see the values and also because I implemented this with a generic handler. Ok so what is next? Well next I need to be able to select items and move them around, but thats for another article.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/10/enumerating-through-an-xml-document/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Growing into Learning LINQ to XML</title>
		<link>http://www.fremus.co.za/blog/2009/10/growing-into-learning-linq-to-xml/</link>
		<comments>http://www.fremus.co.za/blog/2009/10/growing-into-learning-linq-to-xml/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 06:50:26 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ to XML]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=479</guid>
		<description><![CDATA[So I forced myself last night to spend time learning LINQ to XML, both from a technical perspective and a theoretical perspective. My aim is to not only know what LINQ to XML is but also to understand why and how. My goal for the next six months is to not skim over source code, [...]]]></description>
			<content:encoded><![CDATA[<p>So I forced myself last night to spend time learning LINQ to XML, both from a technical perspective and a theoretical perspective. My aim is to not only know what LINQ to XML is but also to understand why and how. My goal for the next six months is to not skim over source code, but to actually spend time going through it and trying to understand it, and the same applies to LINQ to XML. </p>
<p>I started off with the MSDN article, and my goal is to go through all of the material and to get a good overview of the technology as well as knowing the code that goes with it. So I will try and learn as much of code as possible. </p>
<p>From what I read last night I found a few facts about LINQ to XML interesting:</p>
<ul>
<li>It is a built-in language feature. I already knew this actually. And because it is a built-in language feature it gets to use debugging features as well as be strongly typed</li>
<li>LINQ to XML is equivalent to a redesigned XML DOM. You write query expressions that are equivalent to XQuery and XPath in functionality (not syntax).</li>
<li>It uses something called functional construction</li>
<li>You can create and manipulate XML by saving it, serializing it or sending it over the net</li>
<li>The query expression are SQL-like:<br />
IEnumerable<XElement> items = from items in purchaseOrder.Descendants(&#8220;Items&#8221;)<br />
                                            where (int)items.Element(&#8220;Quantity&#8221;) * (decimal)items.Element(&#8220;USPrice&#8221;) > 100<br />
orderby (string)items.Element(&#8220;PartNumber&#8221;)<br />
select items;<br />
The above sample is taken directly from MSDN
</li>
</ul>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/10/growing-into-learning-linq-to-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

