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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:27:22+00:00 2026-06-15T00:27:22+00:00

I have to read an XML document and insert the values into a List<T>

  • 0

I have to read an XML document and insert the values into a List<T> of my objects.

Class (Result)

+Result
-username
-dob
-answer1
-answer2
-uuid

Below is the XML format structure

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<export exportDate="2012-11-07T12:03:52.823+11:00">
    <survey type="USER" completion="2012-11-07T11:46:52.754+11:00" reference="2012-11-07T11:30:34.680+11:00" year="2012" uuid="226f2aa3-46e6-46ab-8995-7d52eb21d5f4">
        <user xsi:type="USER" created="2012-11-07T11:09:30.409+11:00" dob="08/06/1988" surname="Billy" name="Bob" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
        <subject created="2012-11-07T11:09:30.409+11:00" dob="08/06/1988" surname="Billy" name="Bob"/>
        <version released="1970-01-01T10:00:02.012+10:00" version="1"/>
        <result group="2" rawscore="2.4" metric="1"/>
        <result group="2" rawscore="2.0" metric="2"/>
        <answer score="1" question="6"/>
        <answer score="2" question="7"/>
    </survey>       
</export>

My current progress

I was previously using XmlDocument as I have in the past but now that Im working with Linq im sure this can be done in just a few lines. I dont like the look of the code below, if anyway has some tips please help.

thankyou

    List<Result> results = new List<Result>();

    XmlDocument doc = new XmlDocument();
    doc.Load(filename); 

    XmlNodeList objects = doc.GetElementsByTagName("survey"); 
    foreach (XmlNode o in objects)
    {
        Result result = new Result();

        if (o.Attributes["type"].Value == "USER" || o.Attributes["type"].Value == "ADMIN")
        {
            result.surveycompleted = o.Attributes["completion"].Value;

            XmlNodeList usernodes = o.SelectNodes("user");
                ....
            if (usernodes.Count > 0)
            {}
            else
            { 
  • 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-15T00:27:25+00:00Added an answer on June 15, 2026 at 12:27 am

    Use LINQ2XML:

    XElement doc=XElement.Load(filename);
    List<Result> lstSurvey=doc.DescendantsAndSelf("Survey").Select(x=>
    new Result
    {
        uuid=x.Element("Survey").Attribute("uuid").Value,
        username=x.Element("user").Attribute("name").Value,
        dob=x.Element("user").Attribute("dob").Value,
        answer1=x.Elements("answer").First().Value,
        answer2=x.Elements("answer").Skip(1).First().Value
    }
    ).ToList<Result>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this XML document I am trying to read in and insert into
I have a project where I read a xml document into memory. Then I
I have tried to read XML document and i have found a method of
I have an existing List<MyObject> containing 15 MyObject(s). I want to read an XML
I have a class that goes to a URL and gets a xml document
I have a problem when using simplexml to read a xml document that I
Hello I am trying to read my xml document using xpath. I have been
I have an XML document that Im trying to read but my query keeps
I have read som xml into an msxml.IXMLDOMDocument object. However, there is a utility
I have code that creates an XML document that is difficult to read in

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.