Avatar of Tagyourareit
Tagyourareit
 asked on

Parse XML to class and read data fromit

Hello experts,

could someone please help me with my begginers problem?

I code that reads XML and stores it into a class.

Now i am struuggeling how to read data from that class. What i need is accessing data in class based on
attribute "id".

My code

 class Program
    {
        public static string Document = "";
        
        static void Main(string[] args)
        {

            Program.XmlParse();

            Console.ReadLine();
        }



        public static void XmlParse()
        {
            XDocument doc = XDocument.Parse(Document);
            IEnumerable<XmlParseObject> result = from c in doc.Descendants("HotSpot")
                                                 select new XmlParseObject()
                                                 {
                                                     Id = (int)(c.Attribute("id")),
                                                     x = (float)c.Attribute("x"),
                                                     y = (float)c.Attribute("y"),
                                                     z = (float)c.Attribute("z")
                                                 }; 
        }
    
    }


    public class XmlParseObject
    {
        public float x { get; set; }
        public float y { get; set; }
        public float z { get; set; }
        public int Id  { get; set; }
    }

Open in new window


And my XML file:

<?xml version="1.0" encoding="utf-8"?>
<start>
	 <HotSpot id="1" x="67216.14" y="-75172.24" z="-2984.153"/>
	 <HotSpot id="2" x="66326.78" y="-74404.68" z="-3172.191"/>
	 <HotSpot id="3" x="66220.13" y="-73542.97" z="-3286.478"/>
</start>

Open in new window


Thank you!
C#

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61