Check if values exist and for Null values when parsing XML?

How can I check if these values exist and if they are null? Here is the current code (as far as I could get with it):

Invoices.InvoiceRow[] invoiceRow = order.Elements(ns + "InvoiceRows")
.Elements(ns + "InvoiceRow")
.Select(e => new Invoices.InvoiceRow
ProductId = e.Element(ns + "ProductId").IsEmpty ? 0 : (int)e.Element(ns + "ProductId"),
RowId = e.Element(ns + "RowId").IsEmpty ? 0 : (int)e.Element(ns + "RowId"),
Price = e.Element(ns + "Price").IsEmpty ? decimal.Zero : (decimal)e.Element(ns + "Price"),
Name = e.Element(ns + "Name").IsEmpty ? string.Empty : (string)e.Element(ns + "Name"),
DiscountRate = e.Element(ns + "DiscountRate").IsEmpty ? decimal.Zero : (decimal)e.Element(ns + "DiscountRate"),
Quantity = e.Element(ns + "Quantity").IsEmpty ? decimal.Zero : (decimal)e.Element(ns + "Quantity"),
Cost = e.Element(ns + "Cost").IsEmpty ? decimal.Zero : (decimal)e.Element(ns + "Cost"),
InPrice = e.Element(ns + "InPrice").IsEmpty ? decimal.Zero : (decimal)e.Element(ns + "InPrice"),

Open in new window

Jordan JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Jordan;

To test to see if the node exist or not use == null. To see if that node has a value in the inner text use the IsEmpty. Also each element is a XML element and so if you want the inner text for that node use the Value property. The following modified code should work if I understand the question correctly.

Invoices.InvoiceRow[] invoiceRow = 
  order.Elements(ns + "InvoiceRows").Elements(ns + "InvoiceRow")
       .Select(e => new Invoices.InvoiceRow
               ProductId = e.Element(ns + "ProductId") == null ? 0 : int.Parse(e.Element(ns + "ProductId").Value),
               RowId = e.Element(ns + "RowId") == null ? 0 : int.Parse(e.Element(ns + "RowId").Value),
               Price = e.Element(ns + "Price") == null ? decimal.Zero : decimal.Parse(e.Element(ns + "Price").Value),
               Name = e.Element(ns + "Name") == null ? string.Empty : e.Element(ns + "Name").Value,
               DiscountRate = e.Element(ns + "DiscountRate") == null ? decimal.Zero : decimal.Parse(e.Element(ns + "DiscountRate").Value),
               Quantity = e.Element(ns + "Quantity") == null ? decimal.Zero : decimal.Parse(e.Element(ns + "Quantity").Value),
               Cost = e.Element(ns + "Cost") == null ? decimal.Zero : decimal.Parse(e.Element(ns + "Cost").Value),
               InPrice = e.Element(ns + "InPrice") == null ? decimal.Zero : decimal.Parse(e.Element(ns + "InPrice").Value)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.