Selecting specific items from XML and storing them in a list

I am trying to select all elements in an xml file that are a specific surveyid - in this case lets say 1 and get the ChartDataTime,FilteredDataPoint,rawDataPoint,Centroid and CentroidOffset ordered by the ChartDataUID and save them to a list
with this struct as its constructor. How do I do that? AnyHelp would be appreciated
 public struct ChartData
    {
        public int ChartDataUID { get; set; }
        public int JobID { get; set; }
        public int SurveyID { get; set; }
        public DateTime ChartDataTime { get; set; }
        public float rawDataPoint { get; set; }
        public float filteredDataPoint { get; set; }
        public float Centroid { get; set; }
        public int CentroidOffset { get; set; }
    }

Open in new window


<?xml version="1.0" encoding="utf-8"?>
<Surveys>
  <Survey>
    <SurveyID>1</SurveyID>
    <ChartDataUID>2484525</ChartDataUID>
    <ChartDataTime>7/15/2015 11:01:18 AM</ChartDataTime>
    <FilteredDataPoint>256.1976</FilteredDataPoint>
    <rawDataPoint>1268.744</rawDataPoint>
    <Centroid>-2</Centroid>
    <CentroidOffset>0</CentroidOffset>
  </Survey>
  <Survey>
    <SurveyID>1</SurveyID>
    <ChartDataUID>2484526</ChartDataUID>
    <ChartDataTime>7/15/2015 11:01:18 AM</ChartDataTime>
    <FilteredDataPoint>419.4842</FilteredDataPoint>
    <rawDataPoint>1270.254</rawDataPoint>
    <Centroid>-2</Centroid>
    <CentroidOffset>0</CentroidOffset>
  </Survey>
  <Survey>
    <SurveyID>1</SurveyID>
    <ChartDataUID>2484527</ChartDataUID>
    <ChartDataTime>7/15/2015 11:01:18 AM</ChartDataTime>
    <FilteredDataPoint>577.8748</FilteredDataPoint>
    <rawDataPoint>1270.294</rawDataPoint>
    <Centroid>-2</Centroid>
    <CentroidOffset>0</CentroidOffset>
  </Survey>
</Surveys>

Open in new window

LVL 6
r3nderAsked:
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.

ste5anSenior DeveloperCommented:
E.g.

 
namespace ConsoleCS
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    [XmlRoot("Survey")]
    public struct ChartData
    {
        public int ChartDataUID { get; set; }
        public int JobID { get; set; }
        public int SurveyID { get; set; }
        public string ChartDataTime { get; set; }
        public float rawDataPoint { get; set; }
        public float filteredDataPoint { get; set; }
        public float Centroid { get; set; }
        public int CentroidOffset { get; set; }

        public override string ToString()
        {
            return string.Format("{0}, {1}, {2}", this.ChartDataUID, this.JobID, this.SurveyID);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string surveysString = @"<Surveys>
                  <Survey>
                    <SurveyID>1</SurveyID>
                    <ChartDataUID>2484525</ChartDataUID>
                    <ChartDataTime>7/15/2015 11:01:18 AM</ChartDataTime>
                    <FilteredDataPoint>256.1976</FilteredDataPoint>
                    <rawDataPoint>1268.744</rawDataPoint>
                    <Centroid>-2</Centroid>
                    <CentroidOffset>0</CentroidOffset>
                  </Survey>
                  <Survey>
                    <SurveyID>1</SurveyID>
                    <ChartDataUID>2484526</ChartDataUID>
                    <ChartDataTime>7/15/2015 11:01:18 AM</ChartDataTime>
                    <FilteredDataPoint>419.4842</FilteredDataPoint>
                    <rawDataPoint>1270.254</rawDataPoint>
                    <Centroid>-2</Centroid>
                    <CentroidOffset>0</CentroidOffset>
                  </Survey>
                  <Survey>
                    <SurveyID>1</SurveyID>
                    <ChartDataUID>2484527</ChartDataUID>
                    <ChartDataTime>7/15/2015 11:01:18 AM</ChartDataTime>
                    <FilteredDataPoint>577.8748</FilteredDataPoint>
                    <rawDataPoint>1270.294</rawDataPoint>
                    <Centroid>-2</Centroid>
                    <CentroidOffset>0</CentroidOffset>
                  </Survey>
                </Surveys>";

            List<ChartData> chartData = new List<ChartData>();
            XmlSerializer serializer = new XmlSerializer(typeof(ChartData));
            XDocument surveysXml = XDocument.Load(new StringReader(surveysString));
            foreach (var survey in surveysXml.Descendants("Survey"))
            {
                Console.WriteLine(survey.ToString());
                chartData.Add((ChartData)serializer.Deserialize(XmlReader.Create(new StringReader(survey.ToString()))));
            }

            foreach (ChartData data in chartData)
            {
                Console.WriteLine(data.ToString());
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Open in new window


But it's not perfect, cause for example I'm using some disposable classes.
r3nderAuthor Commented:
how would you specify the surveyed - lets say I wanted surveyed 2 or 7  (basically  programatically setting which surveyeid  to retrieve ) because in the list could be groups of surveys with surveyids 1 through 100
would it be in this line or ?
  foreach (var survey in surveysXml.Descendants("Survey"))WHERE(
CuteBugCommented:
It should be

string idToSearch = "1";
foreach (var survey in surveysXml.Descendants("Survey").Where(x => x.Element("SurveyID").Value == idToSearch );

Open in new window

ste5anSenior DeveloperCommented:
That's where Linq is of use:

            foreach (XElement survey in surveysXml.Descendants("Survey")
                .Where(s => s.Descendants("SurveyID").FirstOrDefault().Value == "2"))
            {
                Console.WriteLine(survey.ToString());
                chartData.Add((ChartData)serializer.Deserialize(XmlReader.Create(new StringReader(survey.ToString()))));
            }

Open in new window

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
r3nderAuthor Commented:
Thank you Sir
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
Programming

From novice to tech pro — start learning today.