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 8415329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:24:55+00:00 2026-06-10T01:24:55+00:00

I am trying to parse data from XDocument instance using Linq to XML and

  • 0

I am trying to parse data from XDocument instance using Linq to XML and store it in IEnumerable<MyClass>. I do not want to get every element from the xml document, so I am using ElementAt(#index) to get the value. However sometimes the element that I want is not in the document and then I get index out of range exception. Is there a better way to parse values from this document? Here is the code:

XML Document looks like this:

<someotherelement>   /* I dont want the "someotherelement" part either */
   <subelement></subelement>
   <subelement></subelement>
</someotherelement>
<result>
   <doc>
      <int name="personid">8394</int> /* I don't want this value so I am skipping it */
      <str name="name">James</str>
      <str name="address">30 Awesome Lane</str>
      <arr name="randomearray"> /* I want to skip this too */
         <str name="random1">SomeValue</str>
         <str name="random2">SomeValue</str>
         <str name="random2">SomeValue</str>
      </arr>
      <str name="city">Awesome City</str>
      <str name="state">CA</str>
      <int name="zipcode">84392</int>
      <str name="country">USA</str>
      <str name="phonenumber">8309933820</str>
      <date name="reportdate">2012-07-27T06:01:05.256Z</date>
   </doc>

   /* This is missing address and country */
   <doc>
      <int name="personid">10394</int> /* I don't want this value so I am skipping it */
      <str name="name">Mathew</str>
      <arr name="randomearray"> /* I want to skip this too */
         <str name="random1">SomeValue</str>
         <str name="random2">SomeValue</str>
         <str name="random2">SomeValue</str>
      </arr>
      <str name="city">Not Awesome City</str>
      <str name="state">CA</str>
      <str name="zipcode">58439</str>
      <str name="phonenumber">8309933820</str>
      <date name="reportdate">2012-07-27T06:01:05.256Z</date>
   </doc>
</result>    

Class:

public class Doc
{
    public string Name {get;set;}
    public string Address{get;set;}
    public string City {get;set;}
    public string State {get;set;}
    public int ZipCode {get;set;}
    public string Country {get;set;}        
    public string PhoneNumber {get;set;}
    public DateTime ReportDate {get;set;}
}

Linq To Xml

string pathToXml = "http://www.foo.com/something";
var doc = XDocument.Load(pathToXml);
IEnumerable<Doc> myDoc = from item in doc.Descendants("doc")
                 select new Doc
                 {
                   Name = item.Element("str").Value,
                   Address = item.Element("str").ElementsAfterSelf("str").First().Value,
                   City = item.Element("str").ElementsAfterSelf("str").ElementAt(1).Value,
                   State = item.Element("str").ElementsAfterSelf("str").ElementAt(2).Value,
                   ZipCode = Convert.ToInt32(item.Element("int").Value)
                   Country = item.Element("str").ElementsAfterSelf("str").ElementAt(3).Value,
                   PhoneNumber = item.Element("str").ElementsAfterSelf("str").ElementAt(4).Value,
                   ReportDate = Convert.ToDateTime(item.Element("date").Value)
                 };
  • 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-10T01:24:56+00:00Added an answer on June 10, 2026 at 1:24 am

    You may want to employ XPath which allows you to specify the elements more comfortably, using for example the values of their attributes, which seems useful in your case.

    Combined with an extension method to avoid repetitive statements:

    public static string GetElementValue(this XElement element, string query)
    {
        var elem = element.XPathSelectElement(query);
        return elem == null ? null : elem.Value;
    }
    

    An example:

    select new Doc
    {
        Name = item.GetElementValue("str[@name='name']"),
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract some data from XML using Linq to XML, and I
I am trying to parse data from the XML below (I shortened the data
I am trying to parse data from commoncrawl.org using hadoop streaming. I set up
I'm trying to parse some XML data using jQuery, and as it stands I
I'm trying to parse XML data stored in a variable, not a file. The
I trying to parse xml from an inputstream using the sax parser. The inputstream
I am trying to unmarshall data from an XML object in Java using SimpleXML.
I am Trying to export xml from some data in C# XDocument doc =
I'm trying to get the weather data from googles weather api and parse the
I am trying to parse some data from ifconfig output with sed, but I

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.