C# parsing XMl to an object and reading data from another function

Hello,

This is bassically a follow up question which i asked yesterday.

What i have so far is a function that will parse XML data in an object.

Now the question is how do i access that data from another function:

   class Program
    {
     
        public static XDocument doc = null;

        static void Main(string[] args)
        {
            Program.XmlParse();

            Console.ReadLine();
        }


        public static void XmlParse()
        {

            doc = XDocument.Load("HotSpot.xml");

            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")
                                                 };

            foreach (XmlParseObject obj in result)
            {
                Console.WriteLine("{0,-4}{1,-10}{2,-11}{3,-8}",
                    obj.Id, obj.x, obj.y, obj.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 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



Now i would like to know how would a function look like that would be able to access data i loaded in class XmlParseObject
with method XmlParse.

Thank you and excuse me for posting same issue over again.
TagyourareitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Tagyourareit;

This modification to your code shows one way to do that you want. This makes the results of the parsing of the XML a global variable and therefore available to all code in this assembly. Note I also moved the printing of the objects to its own function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        // I changed this from a string to a XDocument so the document can be read from the file system
        public static XDocument doc = null;
        private static IEnumerable<XmlParseObject> result = null;

        static void Main(string[] args)
        {
            Program.XmlParse();
            DisplayXmlParsedObjects();
            Console.ReadLine();
        }


        public static void XmlParse()
        {
            // Load the XML from the file system seeming it is a file.
            // I have named the xml file HotSpot.xml and because it has
            // no path info it looks for the file in the bin direcotry
            doc = XDocument.Load("HotSpot.xml");

            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 static void DisplayXmlParsedObjects()
        {
            if (result != null)
            {
                // Enumerate through the XmlParseObject objects and displey the info 
                foreach (XmlParseObject obj in result)
                {
                    Console.WriteLine("{0,-4}{1,-10}{2,-11}{3,-8}",
                        obj.Id, obj.x, obj.y, obj.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

0
Fernando SotoRetiredCommented:
Hi Tagyourareit;

Here is a solution more in tune with what the question was asking and that is calling the Parse function from another function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        // I changed this from a string to a XDocument so the document can be read from the file system
        public static XDocument doc = null;

        static void Main(string[] args)
        {
            DisplayXmlParsedObjects();
            Console.ReadLine();
        }


        public static IEnumerable<XmlParseObject> XmlParse()
        {
            // Load the XML from the file system seeming it is a file.
            // I have named the xml file HotSpot.xml and because it has
            // no path info it looks for the file in the bin direcotry
            doc = XDocument.Load("HotSpot.xml");

            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")
                                                 };

            return result;
        }

        public static void DisplayXmlParsedObjects()
        {
            IEnumerable<XmlParseObject> results = XmlParse();

            if (results != null)
            {
                // Enumerate through the XmlParseObject objects and displey the info 
                foreach (XmlParseObject obj in results)
                {
                    Console.WriteLine("{0,-4}{1,-10}{2,-11}{3,-8}",
                        obj.Id, obj.x, obj.y, obj.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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TagyourareitAuthor Commented:
Awesome that what i allways wanted to know how to use data in class like this.

Thank you!
0
Fernando SotoRetiredCommented:
Not a problem Tagyourareit, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.