Solved

Parsing XML using LINQ

Posted on 2011-09-21
5
261 Views
Last Modified: 2013-12-17
Just started using LINQ as it seems very powerful at the moment but a little difficult to get in to.  Consider the following XML...

<?xml version="1.0"?>
<whereabouts date="15/9/2011" gangid="GB">
<job postcode="SG5 1EH" address="my address">
<patch id="25168" status="" surface_id="6mm Ashpalt" />
<patch id="25169" status="" surface_id="6mm Ashpalt" />
</job> 
<job postcode="SG5 2EH" address="17 High Street" >
<patch id="25198" surface_id="6mm Ashpalt />
<patch id="25199" surface_id="6mm Ashpalt/>
</job>
</whereabouts>

Open in new window


I have an object Job which contains a List<Patch> where Patch is another object.  Firstly I want to be able to read just the 'whereabouts' node so that I can perform a quick sanity check - how do you select a single node like this?

Secondly I would like to read all of the jobs and patches into objects.  I know that I could do something like :-

 
List<Job> jobs =
	(from job in xmlDoc.Descendants("job")
	select new Job
	{
	Postcode = job.Element("postcode").Value,
	Address = job.Element("address").Value,
	}).ToList<Job>();

Open in new window


To read the jobs into a list of <Job> objects but is it possible to read the 'Patches' sub-nodes and directly populate the list of patches within each job?  If not how can I associate the patches with the job?
0
Comment
Question by:ChrisMDrew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36578019
I believe that you are looking for something like this:


List<Job> jobs =
	(from job in xmlDoc.Descendants("job")
	select new Job
	{
    	    Postcode = job.Element("postcode").Value,
	    Address = job.Element("address").Value,
            PatchList = job.Elements("patch"),
	}).ToList<Job>();

Open in new window

0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 300 total points
ID: 36578403
I hope your problem selecting subnodes (patches) as well. I've came accross the same kind of problem and you could find your solution there.

 http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27305052.html?cid=1135#a36535232

Good luck!!
0
 
LVL 3

Assisted Solution

by:nixkuroi
nixkuroi earned 200 total points
ID: 36578570
First set up your classes like this:

        public class Patch
        {
            public int ID { get; set; }
            public string Status { get; set; }
            public string SurfaceID { get; set; }
        }

        public class Job
        {
            public string Address { get; set; }
            public string PostCode { get; set; }
            public List<Patch> Patches = new List<Patch>();
        }

Open in new window


Then, you can read your xml and parse it into your List<Job> like this:

XElement jobsElem= XElement.Parse("<whereabouts date=\"15/9/2011\" gangid=\"GB\"><job postcode=\"SG5 1EH\" address=\"my address\"><patch id=\"25168\" status=\"\" surface_id=\"6mm Ashpalt\" /><patch id=\"25169\" status=\"\" surface_id=\"6mm Ashpalt\" /></job> <job postcode=\"SG5 2EH\" address=\"17 High Street\" ><patch id=\"25198\" surface_id=\"6mm Ashpalt\" /><patch id=\"25199\" surface_id=\"6mm Ashpalt\"/></job></whereabouts>");

List<Job> Jobs = new List<Job>();
jobsElem.Elements("job").ToList().ForEach(f =>
{

    string postcode = (f.Attribute("postcode") != null && !String.IsNullOrWhiteSpace(f.Attribute("postcode").Value)) ? f.Attribute("postcode").Value : string.Empty;
    string address = (f.Attribute("address") != null && !String.IsNullOrWhiteSpace(f.Attribute("address").Value)) ? f.Attribute("address").Value : string.Empty;
    Job job = new Job() { Address = address, PostCode = postcode };
    if (f.HasAttributes)
    {
        f.Elements("patch").ToList().ForEach(p =>
        {
            int id = 0;
            if (p.Attribute("id") != null && !String.IsNullOrWhiteSpace(p.Attribute("id").Value))
                Int32.TryParse(p.Attribute("id").Value, out id);
            string status = (p.Attribute("status")!=null && !String.IsNullOrWhiteSpace(p.Attribute("status").Value))? p.Attribute("status").Value: string.Empty;
            string surfaceid = (p.Attribute("surface_id") != null && !String.IsNullOrWhiteSpace(p.Attribute("surface_id").Value)) ? p.Attribute("surface_id").Value : string.Empty;
            job.Patches.Add(new Patch() { ID = id, Status = status, SurfaceID = surfaceid });
        });
    }
    Jobs.Add(job);
});

Open in new window

0
 
LVL 5

Expert Comment

by:Jeroen Timmermans
ID: 36579068
Another route is to serialize the XML to the Job-object.
0
 

Author Closing Comment

by:ChrisMDrew
ID: 36579207
Thanks to all - this has made my business object very easy to generate from the XML.  Adding checks to ensure that the attributes exist is definately a good idea also.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question