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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:51:11+00:00 2026-05-12T16:51:11+00:00

I am trying to load data from XML string into a structure of some

  • 0

I am trying to load data from XML string into a structure of some sort so once loaded I can then say Data.PropertyName to read the values.

Is the below code the most optimal way of loading the data into the structure?

Obviously calling First() has a memory hit so if you have elements with sub elements will calling First() for each one become an issue?

Thanks

    string xml = @"<ROOT>
                   <ID>1</ID>
                   <NAME>RF1</NAME>
                   <STAT>10200</STAT>
                   <TEST>
                       <ID>1</ID>
                       <NAME>BIGUN</NAME>
                   </TEST>
                   </ROOT>
                   ";

     XElement Data = XElement.Parse(xml);


     var Element = (from p in Data.Descendants("ROOT") 
                   select new { 
                        ID = (int)p.Element("ID"), 
                        Test = new { 
                                    ID = p.Element("TEST").Descendants("ID").First(), 
                                    NAME = p.Element("TEST").Descendants("NAME").First() 
                                 }, 
                        Stat = p.Element("STAT") }).First();

//Read Element.ID, Element.Test.Name
  • 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-12T16:51:11+00:00Added an answer on May 12, 2026 at 4:51 pm

    You could use XmlSerializer to deserialize it?

    using System;
    using System.IO;
    using System.Xml.Serialization;
    
    [XmlRoot("ROOT")]
    public class MyType
    {
        [XmlElement("ID")]
        public string Id { get; set; }
        [XmlElement("NAME")]
        public string Name { get; set; }
        [XmlElement("STAT")]
        public string Stat { get; set; }
        [XmlElement("TEST")]
        public MyOtherType Nested { get; set; }
    }
    public class MyOtherType
    {
        [XmlElement("ID")]
        public string Id { get; set; }
        [XmlElement("NAME")]
        public string Name { get; set; }
    }
    static class Program
    {
    
        static void Main()
        {
            string xml = @"<ROOT>
                       <ID>1</ID>
                       <NAME>RF1</NAME>
                       <STAT>10200</STAT>
                       <TEST>
                           <ID>1</ID>
                           <NAME>BIGUN</NAME>
                       </TEST>
                       </ROOT>";
            MyType obj = (MyType) new XmlSerializer(typeof(MyType))
                .Deserialize(new StringReader(xml));
            Console.WriteLine(obj.Id);
            Console.WriteLine(obj.Name);
            Console.WriteLine(obj.Stat);
            Console.WriteLine(obj.Nested.Id);
            Console.WriteLine(obj.Nested.Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 220k
  • Answers 220k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Having had a quick skim read of the Wikipedia article… May 12, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer I will show you a simple way to check this… May 12, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer No, there is not. (And that's because such an element… May 12, 2026 at 11:54 pm

Related Questions

I am working on a small project that is receiving XML data in string
I seem to always have problems with converting data to and from XML in
I seems to run into problem, and not sure how to make it work.
I am parsing an xml file using php simplexml_load_file() and then inserting the desired

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.