Parse XML using LINQ

Posted on 2011-10-12
Last Modified: 2013-12-16
I have an XML document which has been sent via a web service which I want to parse into an Object using Linq.

My goal is to parse the XML into a LIST of <Job> objects - each jobn has a list or <Patch> objects.  My LINQ query is :-

var jobs = (from job in xDocument.Descendants("job")
select new Job()
	ID	 = int.Parse(job.Attribute("id").Value),
	Location = job.Attribute("address").Value,
	Postcode = job.Attribute("postcode").Value,
	Patches = (from patch in job.Descendants("patch")
		select new Patch()
		ID	= int.Parse(patch.Attribute("id").Value),
		Depth		= decimal.Parse(patch.Attribute("d").Value),

Open in new window

This seemed to work perfectly until I realised that the 'Depth' fiield for a patch can be "" (an empty string.  This causes the query to fall over as it tries to convert an empty string to a decimal.

Is it possible to add more intelligence to the query to check for this?
Question by:ChrisMDrew
    LVL 6

    Expert Comment

    You can try this
    Depth		= patch.Attribute("d").Value == string.Empty ? 0 : decimal.Parse(patch.Attribute("d").Value),

    Open in new window

    This assigns 0 to Depth if patch.Attribute("d").Value is an empty string. Otherwise it will parse the value and assign that to Depth.

    Author Comment

    Thanks - I did try something similar but used the C# syntax of putting brackets around the check - doesn't work in LINQ!  I have a similar problem when a field is not specified at all in the XML - for example in the above query sometimes XML is not specified.  Can I check for an attribute being specified in a similar way?  Maybe

    Postcode      = job.Attribute("postcode") == null ? ""  job.Attribute("postcode").Value

    or is there a better way?
    LVL 6

    Accepted Solution

    Looks good to me. Just don't forget the colon before the second expression
    Postcode      = job.Attribute("postcode") == null ? "" : job.Attribute("postcode").Value

    Open in new window

    You'll probably want to put that in a method, if you need it a lot. Something along those lines:
    private string EvaluateAttribute(XAttribute att)
        return att == null ? string.Empty : att.Value;

    Open in new window

    and call it everywhere, where you need a string from an attribute, like in
    Postcode      = EvaluateAttribute(job.Attribute("postcode"))

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now