xml to a list

I have an error adding my desterilized xml data to a list - anyone see the error?
  List<InvictusRTDatabase.ChartData> chartData = new List<InvictusRTDatabase.ChartData>();
            string path = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Invictus" + "\\" + jobid + "\\" + jobid+ ".xml";
             XmlSerializer serializer = new XmlSerializer(typeof(InvictusRTDatabase.ChartData));
            XDocument surveysXml = XDocument.Load(path
            string idToSearch = SurveyID.ToString();

            foreach (var survey in surveysXml.Descendants("Survey").Where(x => x.Element("SurveyID").Value == idToSearch))
            {
                chartData.Add((InvictusRTDatabase.ChartData)serializer.Deserialize(XmlReader.Create(new StringReader(survey.ToString()))));//new StringReader(survey.ToString() <-------------------------------------HERE IS WHERE IT FAILS
            }

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:
Please post the error message.
ste5anSenior DeveloperCommented:
It's better to split it for debugging purposes:

foreach (var survey in surveysXml.Descendants("Survey").Where(x => x.Element("SurveyID").Value == idToSearch))
{
	string surveyString = survey.ToString();
	StringReader stringReader = new StringReader(surveyString);
	XmlReader xmlReader = XmlReader.Create(stringReader);
	InvictusRTDatabase.ChartData cd = (InvictusRTDatabase.ChartData)serializer.Deserialize(xmlReader);
	chartData.Add(cd);
}

Open in new window

CuteBugCommented:
If survery.ToString() is failing, it might be because survey is null.

add a null check for survey before executing your code and as ste5an said, split your statement into several statements for easy debuggability.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

r3nderAuthor Commented:
the error is  "There is an error in XML document (1, 2)." after I put a try catch in
I think it might have to do with below - it is located in a separate class
 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; }
        public override string ToString()
        {
            return string.Format("{0}, {1}, {2}", this.ChartDataUID, this.JobID, this.SurveyID); <----THIS
        }
    }

Open in new window

r3nderAuthor Commented:
           List<InvictusRTDatabase.ChartData> chartData = new List<InvictusRTDatabase.ChartData>();
            string path = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Invictus" + "\\" + jobid + "\\" + jobid+ ".xml";
             XmlSerializer serializer = new XmlSerializer(typeof(InvictusRTDatabase.ChartData));
            XDocument surveysXml = XDocument.Load(path);//new StringReader(path)
            string idToSearch = SurveyID.ToString();

            foreach (var survey in surveysXml.Descendants("Survey").Where(x => x.Element("SurveyID").Value == idToSearch))
            {
                try
                {
                    if (survey != null)
                    {
                        chartData.Add((InvictusRTDatabase.ChartData)serializer.Deserialize(XmlReader.Create(new StringReader(survey.ToString()))));//new StringReader(survey.ToString()
                    }
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }
            }

Open in new window

ste5anSenior DeveloperCommented:
Yup, as your question is a f'up, take a look at my samples again.

You need classes and not structs as target and you need to specify at least the XmlRoot() attribute, cause the name ChartData does not match the XML fragments root <survey>.

Otherwise you need to implement ISerializable for your struct.
r3nderAuthor Commented:
the  error now is "There is an error in XML document (4, 55)."
ste5anSenior DeveloperCommented:
Please post a concise and complete example. It's hard to guess.. Craft a small console application and post a sample XML.
r3nderAuthor Commented:
at the bottom of my form I placed this
   [XmlRoot("Survey")]
    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; }
        public override string ToString()
        {
            return string.Format("{0}, {1}, {2}", this.ChartDataUID, this.JobID, this.SurveyID);
        }

Open in new window

I changed my code in the form to this
            List<InvictusRTDatabase.ChartData> chartData = new List<InvictusRTDatabase.ChartData>();
            string path = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Invictus" + "\\" + jobid + "\\" + jobid+ ".xml";
             XmlSerializer serializer = new XmlSerializer(typeof(ChartData));<-- POINTS TO CHART DATA HERE AT THE BOTTOM
            XDocument surveysXml = XDocument.Load(path);//new StringReader(path)
            string idToSearch = SurveyID.ToString();

            foreach (var survey in surveysXml.Descendants("Survey").Where(x => x.Element("SurveyID").Value == idToSearch))
            {
                try
                {
                    if (survey != null)
                    {
                        chartData.Add((InvictusRTDatabase.ChartData)serializer.Deserialize(XmlReader.Create(new StringReader(survey.ToString()))));
                    }
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }
            }

Open in new window

I don't get whats wrong?
ste5anSenior DeveloperCommented:
Forget my comment about structs. That was nonsense.

Take a closer look at my samples again.

ChartDataTime must be a string. The value in the XML is not a serialized DateTime value:

public string ChartDataTime { get; set; }

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
ste5anSenior DeveloperCommented:
btw, you haven't took a look at the exception, haven't you? Cause it tells you that:

Capture.PNG
r3nderAuthor Commented:
ok so,
I changed the struct at the bottom of the page to
    [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);
        }
    }

Open in new window

and my code to add it to the chart is like so
            List<ChartData> chartData = new List<ChartData>();
            string path = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Invictus" + "\\" + jobid + "\\" + jobid+ ".xml";
             XmlSerializer serializer = new XmlSerializer(typeof(ChartData));
            XDocument surveysXml = XDocument.Load(path);//new StringReader(path)
            string idToSearch = SurveyID.ToString();

            foreach (var survey in surveysXml.Descendants("Survey").Where(x => x.Element("SurveyID").Value == idToSearch))
            {
                try
                {
                    if (survey != null)
                    {
                        chartData.Add((ChartData)serializer.Deserialize(XmlReader.Create(new StringReader(survey.ToString()))));
                    }
                    
                }
                catch (Exception ex)
                {
                    string s = ex.InnerException.ToString();
                }
            }
            int MyCount = 0;
            for (int i = 0; i < chartData.Count(); i++)
            {
                DateTime date = Convert.ToDateTime(chartData[i].ChartDataTime).AddMilliseconds(250 * MyCount);
                    string strTime = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy HH:mm:ss.fff}", date);
                    string format1 = "MM/dd/yyyy HH:mm:ss.fff";
                    S_filteredP.name = DateTime.ParseExact(strTime, format1, null);
                    S_filteredP.Value = float.Parse(chartData[i].filteredDataPoint.ToString(), CultureInfo.CurrentCulture);
                    S_rawP.name = DateTime.ParseExact(strTime, format1, null);
                    S_rawP.Value = float.Parse(chartData[i].rawDataPoint.ToString(), CultureInfo.CurrentCulture);
                    S_centroidP.name = DateTime.ParseExact(strTime, format1, null);
                    S_centroidP.Value = float.Parse(chartData[i].Centroid.ToString(), CultureInfo.CurrentCulture);
                    rawSeries.DataPoints.Add(new CategoricalDataPoint() { Value = S_rawP.Value, Category = S_rawP.name });
                    filteredSeries.DataPoints.Add(new CategoricalDataPoint() { Value = S_filteredP.Value, Category = S_filteredP.name });
                    centSeries.DataPoints.Add(new CategoricalDataPoint() { Value = S_centroidP.Value, Category = S_centroidP.name });
                    MyCount++;
            }

Open in new window

r3nderAuthor Commented:
thanks
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
XML

From novice to tech pro — start learning today.