<?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; XAttribute</title>
	<atom:link href="http://www.fremus.co.za/blog/tag/xattribute/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fremus.co.za/blog</link>
	<description>Demistifying Life and Web Development</description>
	<lastBuildDate>Wed, 08 Sep 2010 18:18:10 +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>
	</channel>
</rss>
