Fremus.co.za

Demistifying Life and Web Development

Archive for the 'LINQ to XML' Category

Getting a single XAttribute or XElement

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;

To loop through the results you have to use a foreach loop like this:


foreach (XAttribute innerAttrib in attrib)
{
     builder.AppendLine("

" + innerAttrib.Value +"

"); }

If however you just wanted a single attribute result, you could write the same code like this:


 XAttribute attribDisplayStyle = (from att in elemInner.Attributes()
                                            where (string)att.Name.ToString() == "sectionName"
                                            select att).Single();

Then you only have a single XAttribute instance and you dont need a foreach loop:


attribDisplayStyle.Value;

You would apply the same logic to XElement as well.

  • Share/Bookmark
posted by fr3dr1k in C#,LINQ to XML and have No Comments

Getting search results from Bing REST service and using LINQ to process results

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:

            XDocument document = XDocument.Load("http://api.search.live.net/xml.aspx?Appid=&query=sushi&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 testelem = from el in searchItems.Elements()
                                                 select el;
            foreach (XElement myElem in testelem)
            {
                context.Response.Write(myElem.Value + "");
            }

That is very easy!

  • Share/Bookmark
posted by fr3dr1k in Bing,C#,LINQ to XML and have No Comments

Enumerating through an XML document

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 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 here. 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:

The non-generic type ‘System.Collections.IEnumerable’ cannot be used with type arguments

Lets show some code that reads XML:

        XDocument document = XDocument.Load(context.Server.MapPath(@"CVTemplate.xml"));
        XElement root = document.Root;
        IEnumerable 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());

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.

  • Share/Bookmark
posted by fr3dr1k in C#,LINQ to XML and have No Comments

Growing into Learning LINQ to XML

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.

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.

From what I read last night I found a few facts about LINQ to XML interesting:

  • 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
  • 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).
  • It uses something called functional construction
  • You can create and manipulate XML by saving it, serializing it or sending it over the net
  • The query expression are SQL-like:
    IEnumerable items = from items in purchaseOrder.Descendants(“Items”)
    where (int)items.Element(“Quantity”) * (decimal)items.Element(“USPrice”) > 100
    orderby (string)items.Element(“PartNumber”)
    select items;
    The above sample is taken directly from MSDN
  • Share/Bookmark
posted by fr3dr1k in C#,LINQ to XML and have No Comments
Get Adobe Flash playerPlugin by wpburn.com wordpress themes