C# 3.0 and XLinq

I have just been playing around a little with C#3.0 with XLinq trying to see how easy it would be to parse a simple xml document and create a list of strongly type objects out.I had fun doing this and I will show you what I was doing so that you can either:a) learn a little of what I learntb) tell me where I am going wrong, i.e, if there is a simpler method.


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan

<p>I have just been playing around a little with C#3.0 with XLinq trying to see how easy it would be to parse a simple xml document and create a list of strongly type objects out.<p />I had fun doing this and I will show you what I was doing so that you can either:a) learn a little of what I learntb) tell me where I am going wrong, i.e, if there is a simpler method.<p />The XML file I am trying to parse is the MusizMox Style XML Document <a href="http://musicmoz.org/xml/musicmoz.lists.styles.xml"><a href="http://musicmoz.org/xml/musicmoz.lists.styles.xml">http://musicmoz.org/xml/musicmoz.lists.styles.xml</a></a>:<p /><musicmoz><style><name>A Cappella</name><category>Styles/Vocal/A_Cappella</category></style><style><name>Acadian</name><category>Styles/World/Cajun/Acadian</category></style>......<p />And I want to produce a List of Name and Categories.<p />First I created a class as follows:public class TagCategory{public string Name{get{return name;}set{name = value;}}<p />public string Category{get{return category;}set{category = value;}}<p />private string name;private string category;}<p />This is just a really simple class that holds the tag name and the category of the style.<p />Now comes the interesting part. The C#3.0 bit. I used XLinq with this, it is only a couple of lines, but it seems to be pretty cool.<p />Firtly I need to load the XML file, which is simple enough:XDocument xdoc = XDocument.Load("musicmoz.lists.styles.xml");XElement root = xdoc.Element("musicmoz");<p />Now that it is loaded I simple declare a List and run some XLinq as follows:<p />List t = new List(root.Elements("style").Select(a => new TagCategory{ Name = a.Element("name").Value,Category = a.Element("category").Value}));<p />What I can tell is happening is that we are looking at the root element and pulling back all the "style" elements, then for each element Select all of them but return a new TagCategory using object initilaizers for the Name and the Category class properties.<p />Clear as mud! :)<p />Any questions let me know and I will try and answer them :) <a href="mailto:paul.kinlan@gmail.com"><a href="mailto:paul.kinlan@gmail.com">paul.kinlan@gmail.com</a></a><p /></p>


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan


Print Share Comment Cite Upload Translate
APA
Paul Kinlan | Sciencx (2023-03-24T15:54:05+00:00) » C# 3.0 and XLinq. Retrieved from https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/.
MLA
" » C# 3.0 and XLinq." Paul Kinlan | Sciencx - Wednesday May 17, 2006, https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/
HARVARD
Paul Kinlan | Sciencx Wednesday May 17, 2006 » C# 3.0 and XLinq., viewed 2023-03-24T15:54:05+00:00,<https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/>
VANCOUVER
Paul Kinlan | Sciencx - » C# 3.0 and XLinq. [Internet]. [Accessed 2023-03-24T15:54:05+00:00]. Available from: https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/
CHICAGO
" » C# 3.0 and XLinq." Paul Kinlan | Sciencx - Accessed 2023-03-24T15:54:05+00:00. https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/
IEEE
" » C# 3.0 and XLinq." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/. [Accessed: 2023-03-24T15:54:05+00:00]
rf:citation
» C# 3.0 and XLinq | Paul Kinlan | Sciencx | https://www.scien.cx/2006/05/17/c-3-0-and-xlinq/ | 2023-03-24T15:54:05+00:00
https://github.com/addpipe/simple-recorderjs-demo