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

  • SEARCH
  • Home
  • 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 8772975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:10:27+00:00 2026-06-13T18:10:27+00:00

I’m trying to get my head arround Linq to xml so let me jump

  • 0

I’m trying to get my head arround “Linq to xml” so let me jump right to it. I have this xml :

<?xml version="1.0"?>
<PurchaseOrders>
  <PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
    <Address Type="Shipping">
      <Name>Ellen Adams</Name>
      <Street>123 Maple Street</Street>
      <City>Mill Valley</City>
      <State>CA</State>
      <Zip>10999</Zip>
      <Country>USA</Country>
    </Address>
    <Address Type="Billing">
      <Name>Tai Yee</Name>
      <Street>8 Oak Avenue</Street>
      <City>Old Town</City>
      <State>PA</State>
      <Zip>95819</Zip>
      <Country>USA</Country>
    </Address>
    <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>
    <Items>
      <Item PartNumber="872-AA">
        <ProductName>Lawnmower</ProductName>
        <Quantity>1</Quantity>
        <USPrice>148.95</USPrice>
        <Comment>Confirm this is electric</Comment>
      </Item>
      <Item PartNumber="926-AA">
        <ProductName>Baby Monitor</ProductName>
        <Quantity>2</Quantity>
        <USPrice>39.98</USPrice>
        <ShipDate>1999-05-21</ShipDate>
      </Item>
    </Items>
  </PurchaseOrder>
</PurchaseOrders>

And then I created som simple objects :

public class PurchaseOrder
{
    public string PurchaseOrderNumber { get; set; }
    public string OrderDate { get; set; }
    public Address BillingAddress { get; set; }
    public Address ShippingAddress { get; set; }
    public string DeliveryNotes { get; set; }
    public List<Item> Items { get; set; }
}

public class Address
{
    public string Type { get; set; }
    public string Name  { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
}

public class Item
{
    public string PartNumber { get; set; }
    public string ProductName { get; set; }
    public string Quantity { get; set; }
    public string USPrice { get; set; }
    public string Comment { get; set; }
}

Then i go ahead an make my linq :

    var orders = (from order in XDocument.Load(Server.MapPath("/App_Data/Order.xml")).Descendants("PurchaseOrder")
select new PurchaseOrder
{
    PurchaseOrderNumber = order.Attribute("PurchaseOrderNumber").Value,
    OrderDate = order.Attribute("OrderDate").Value,
    BillingAddress = new Address
    {
        City = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single().Element("City").Value,
        Country = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single().Element("Country").Value,
        Name = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single().Element("Name").Value,
        State = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single().Element("State").Value,
        Street = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single().Element("Street").Value,
        Type = "Billing",
        Zip = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single().Element("Zip").Value
    },
    ShippingAddress = new Address
    {
        City = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single().Element("City").Value,
        Country = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single().Element("Country").Value,
        Name = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single().Element("Name").Value,
        State = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single().Element("State").Value,
        Street = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single().Element("Street").Value,
        Type = "Shipping",
        Zip = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single().Element("Zip").Value
    },
    DeliveryNotes = order.Element("DeliveryNotes").Value,
    Items = null
});

First of, why does the “order.Element(“DeliveryNotes”).Value” not exists in the output xml and throws “Object reference not set to an instance of an object” ?

Second, how do I get the “Type” (which is an Attribute) for the address?

Third, how do I make the list of items this one “Items = null” so that I have a fully functional object to work with ?

And finally if the rest of the linq needs som tuning then go ahead and guid me in the right direction ;o)

  • 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-06-13T18:10:29+00:00Added an answer on June 13, 2026 at 6:10 pm

    UPDATE
    I’ve rewrited code so it do not using value property now. It also should populate your Items list.

    I’ve found this topics very useful:

    LINQ to XML optional element query

    How do you guard for Null Reference exceptions in Linq To Xml?

    var doc = XDocument.Load(@"C:\Temp\stackoverflow.xml");
    
    var ordersTemp = 
        (from order in doc.Descendants("PurchaseOrder") select 
                new {
                    order,
                    BillingAddress = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Billing").Single(),
                    ShippingAddress = order.Descendants("Address").Where(a => a.Attribute("Type").Value == "Shipping").Single(),
                    Items = order.Descendants("Items").Descendants("Item")
                });
    
    var orders = (from order in ordersTemp select  
        new PurchaseOrder
        {
            PurchaseOrderNumber = (string)order.order.Attribute("PurchaseOrderNumber"),
            OrderDate = (string)order.order.Attribute("OrderDate"),
            BillingAddress = new Address
            {
                City = (string)order.BillingAddress.Element("City"),
                Country = (string)order.BillingAddress.Element("Country"),
                Name = (string)order.BillingAddress.Element("Name"),
                State = (string)order.BillingAddress.Element("State"),
                Street = (string)order.BillingAddress.Element("Street"),
                Type = "Billing",
                Zip = (string)order.BillingAddress.Element("Zip")
            },
            ShippingAddress = new Address
            {
                City = (string)order.ShippingAddress.Element("City"),
                Country = (string)order.ShippingAddress.Element("Country"),
                Name = (string)order.ShippingAddress.Element("Name"),
                State = (string)order.ShippingAddress.Element("State"),
                Street = (string)order.ShippingAddress.Element("Street"),
                Type = "Shipping",
                Zip = (string)order.ShippingAddress.Element("Zip")
            },
            DeliveryNotes = (string)order.order.Element("DeliveryNotes"),
            Items = (from item in order.Items select new Item
            {
                PartNumber = (string)item.Attribute("PartNumber"),
                ProductName = (string)item.Element("ProductName"),
                Quantity = (string)item.Element("Quantity"),
                USPrice = (string)item.Element("USPrice"),
                Comment = (string)item.Element("Comment")
            }).ToList()
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.