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 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

Related Questions

I am trying to load some XML data into C++ code (classes) using gsoap.
Hello there im trying to save some data from dynamic textboxes into XML file.
I'm facing a rather strange Issue when trying to load data from an XML-Webservice.
I'm trying to load and parse json data from an external source into a
I have an XML file containing seed data that I'm trying to load into
I'm trying to load data from xml-file with TinyXML (c++). int height = rootElem->attrib<int>(height,
I'm trying to load some data from a plist file. It contains an NSArray
I am trying to load some GZIP-ed data from a resource in my .jar,
I am trying to load mysql data from my database from a html select
I'm trying to perform the acquisition of data from the internet on the load

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.