Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6021771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:45:42+00:00 2026-05-23T03:45:42+00:00

I am receiving data from a Web Service. The XML coming in is something

  • 0

I am receiving data from a Web Service. The XML coming in is something like:

<data>
    <item>
        <code>a</code>
        <price>2.89</price>
    </item>
    <item>
        <code>a</code>
        <price>2.89</price>
        <colour>blue</colour>
    </item>
</data>

So, we see that one item has an additional property of Colour.

Okay, this gets converted into a List<item> which is the point at which I get hold of it.

I need to convert this list into an XDocument.

Using:

var xml = new XDocument(
new XDeclaration("1.0", "utf-16", "yes"),
new XElement("data",
from i in myList
select new XElement("item",
new XElement("price", i.price),
new XElement("code", i.code),
new XElement("colour", i.colour))));

(I’ve typed this from memory, so excuse spellings)

Here, it errors because i.colour is null.

How do I cope with this?

Thanks in advance

Griff

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:45:43+00:00Added an answer on May 23, 2026 at 3:45 am

    You’ll want to check whether i.colour is null before trying to access it.

    You can do this neatly using the null-coalescing operator like:

    new XElement("colour", i.colour ?? ""))));
    

    Assuming that you want an empty string as the value if i.colour is null.

    UPDATE

    Based on your comment below, if you don’t want the element added if i.colour is null then create it independantly of the XDocument instantiation and add it as required.

    var xml = new XDocument(...);
    
    if(i.colour != null)
    {
      xml.Add(new XElement(...));
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My iPhone app will be receiving an xml feed from a Java web service.
I'm receiving data from web-services in XML and I'm using that data via objects,
I have this stadard code for receiving a class from a web service: function
I am receiving data from a web service, and some of the strings have
I'm trying to consume data from a 3rd party web service. Unfortunately, the web
I'm trying to parse out some data thats returned from a web service via
I'm designing a system which is receiving data from a number of partners in
We are receiving a message from a WCF call as GZip data, but the
I am receiving a stream of data from a webservice and trying to save
I'm receiving data from an AJAX request, and it's really weird. I thought it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.