Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

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

Avatar of ste5an
ste5an
Flag of Germany image

Please post the error message.
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

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.
Avatar of r3nder

ASKER

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

Avatar of r3nder

ASKER

           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

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.
Avatar of r3nder

ASKER

the  error now is "There is an error in XML document (4, 55)."
Please post a concise and complete example. It's hard to guess.. Craft a small console application and post a sample XML.
Avatar of r3nder

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw, you haven't took a look at the exception, haven't you? Cause it tells you that:

User generated image
Avatar of r3nder

ASKER

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

Avatar of r3nder

ASKER

thanks