Jump to content
Software FX Community

Cannot import chart settings from XML in a stream that was previously exported as XML in a stream.


DanielL_550

Recommended Posts

I am trying to save the chart settings to XML in a memory stream by using the Export method, save the XML to a database, extract the XML from the database, and Import the XML settings back into a chart at a later time. This will allow our users to save their specific settings for multiple charts for multiple series and then retrieve them at a later time. The Export seems to work fine but when I import the XML settings into a chart the settings do not change. I have attached my VS 2005 project and I have listed the basic code below. Can anyone tell me what I am doing wrong? Thank you.

// Set the chart background red.   chart.BackColor = System.Drawing.Color.Red;   // Set the first series tan   chart.Series[0].Color = System.Drawing.Color.Tan;   string xmlPreferences;   // Get the chart preferences with the red background and tan series.   using (System.IO.MemoryStream savedPreferences = new System.IO.MemoryStream())   {   chart.Export(ChartFX.WebForms.FileFormat.Xml, savedPreferences);   // Try flushing the data to the stream to see if this helps.   savedPreferences.Flush();   savedPreferences.Close();   // Convert the stream to a string so we can read the preferences.   // You can see here that the new red background and tan series were exported as XML into   //  the memory stream.   xmlPreferences = System.Text.Encoding.UTF8.GetString(savedPreferences.GetBuffer());   }   // Set the chart background white.   chart.BackColor = System.Drawing.Color.White;   // Set the first series black.   chart.Series[0].Color = System.Drawing.Color.Black;   // Convert the string back to a stream. This is the preferred method so we can save it to a database column.   using (System.IO.MemoryStream restorePreferences = new System.IO.MemoryStream())   {   using (StreamWriter sw = new StreamWriter(restorePreferences))   {   // Write the XML data to the stream.   sw.Write(xmlPreferences);   // Try flushing the data to the stream to see if this helps.   sw.Flush();   restorePreferences.Flush();   // The Import throws and exception if we do this here.   //sw.Close();   // The XML made it to the stream because the background color is red and the series color is tan.   string xmlRestorePreferences = System.Text.Encoding.UTF8.GetString(restorePreferences.GetBuffer());   restorePreferences.Position = 0;   // Import the colors we set above.   // **** THIS DOES NOT SET THE CHART COLORS FOR SOME REASON.   chart.Import(ChartFX.WebForms.FileFormat.Xml, restorePreferences);   }   }   // THIS IS STILL WHITE   System.Drawing.Color backgroundColor = chart.BackColor;   // THIS IS STILL BLACK   System.Drawing.Color seriesColor = chart.Series[0].Color;

Link to comment
Share on other sites

The problem you are having is that the XML you are passing has invalid characters at the end and therefore is not a valid XML document. The reason is that you are using GetBuffer. GetBuffer returns the internal buffer of the memory stream which may be bigger than the actual contents. To obtain the content of the stream you must use ToArray instead:

xmlPreferences = System.Text.

Encoding.UTF8.GetString(savedPreferences.ToArray());

This issue is not related to Chart FX but rather to an incorrect handling of Memory Streams.

Link to comment
Share on other sites

Thank you for catching my stupid mistake Francisco. I am still having a problem so apparently I am still doing something else wrong. I can call the Import method but the colors still do not change. Can you tell me what I am doing wrong? My code has been changed to this now. Thank you.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

  // Set the chart background red.

  chart.BackColor = System.Drawing.Color.Red;

  // Set the first series tan

  chart.Series[0].Color = System.Drawing.Color.Tan;

  string xmlPreferences;

  // Get the chart preferences with the red background and tan series.

  using (System.IO.MemoryStream savedPreferences = new System.IO.MemoryStream())

  {

  chart.Export(ChartFX.WebForms.FileFormat.Xml, savedPreferences);

  // Try flushing the data to the stream to see if this helps.

  savedPreferences.Flush();

  savedPreferences.Close();

  // Convert the stream to a string so we can read the preferences.

  // You can see here that the new red background and tan series were exported as XML into

  //  the memory stream.

  xmlPreferences = System.Text.Encoding.UTF8.GetString(savedPreferences.ToArray());

  }

  // Set the chart background white.

  chart.BackColor = System.Drawing.Color.White;

  // Set the first series black.

  chart.Series[0].Color = System.Drawing.Color.Black;

  // Convert the string back to a stream. This is the preferred method so we can save it to a database column.

  using (System.IO.MemoryStream restorePreferences = new System.IO.MemoryStream())

  {

  using (StreamWriter sw = new StreamWriter(restorePreferences, System.Text.Encoding.UTF8))

  {

  // Write the XML data to the stream.

  sw.Write(xmlPreferences);

  // Try flushing the data to the stream to see if this helps.

  sw.Flush();

  restorePreferences.Flush();

  restorePreferences.Seek(0, SeekOrigin.Begin);

  // Import the colors we set above.

  // **** THIS DOES NOT SET THE CHART COLORS FOR SOME REASON.

  chart.Import(ChartFX.WebForms.FileFormat.Xml, restorePreferences);

  }

  }

  // THIS IS STILL WHITE

  System.Drawing.Color backgroundColor = chart.BackColor;

  // THIS IS STILL BLACK

  System.Drawing.Color seriesColor = chart.Series[0].Color;

Link to comment
Share on other sites

The following code. Exactly as it is works for me in a sample project created from the scratch:

// Set the chart background red.chart.BackColor = System.Drawing.Color.Red;

// Set the first series tanchart.Series[0].Color = System.Drawing.Color.Tan;

string xmlPreferences;

// Get the chart preferences with the red background and tan series.using (System.IO.MemoryStream savedPreferences = new System.IO.MemoryStream())

{

chart.Export(ChartFX.WebForms.
FileFormat.Xml, savedPreferences);

// Try flushing the data to the stream to see if this helps.

savedPreferences.Flush();

savedPreferences.Close();

// Convert the stream to a string so we can read the preferences.

// You can see here that the new red background and tan series were exported as XML into

// the memory stream.xmlPreferences = System.Text.Encoding.UTF8.GetString(savedPreferences.ToArray());

}

// Set the chart background white.chart.BackColor = System.Drawing.Color.White;

// Set the first series black.chart.Series[0].Color = System.Drawing.Color.Black;

// Convert the string back to a stream. This is the preferred method so we can save it to a database column.using (System.IO.MemoryStream restorePreferences = new System.IO.MemoryStream())

{

using (StreamWriter sw = new StreamWriter(restorePreferences))

{

// Write the XML data to the stream.

sw.Write(xmlPreferences);

// Try flushing the data to the stream to see if this helps.

sw.Flush();

restorePreferences.Flush();

restorePreferences.Seek(0,
SeekOrigin.Begin);

// Import the colors we set above.

// **** THIS DOES NOT SET THE CHART COLORS FOR SOME REASON.chart.Import(ChartFX.WebForms.FileFormat.Xml, restorePreferences);

}

}

// THIS IS STILL WHITESystem.Drawing.Color backgroundColor = chart.BackColor;

// THIS IS STILL BLACK

System.Drawing.

Color seriesColor = chart.Series[0].Color;
Link to comment
Share on other sites

Thank you Francisco. I copied the code you had exactly as you have it. If I put the Chart in the Default.aspx like this

<chartfx7:Chart ID="chart" runat="server" Width="925" Height="600"></chartfx7:Chart>

and do not add any data to it, the background setting of Red is successfully imported. If I create the Chart in code like this

Chart chart = new Chart(this);

and do not add any data or change anything else, I get the "Input string was not in a correct format" exception thrown. I am sorry I skipped these lines when I put the code in my original message. I put it in the project I had attached and I expected you to use it for testing. That is my fault.

Do you know why this is happening?

Thank you for your help.

Link to comment
Share on other sites

For some reason if I create the Chart like this

Chart chart = new Chart(this);

I get the "Input string was not in a correct format" exception thrown when I call the Import method.

If I create the Chart like this

Chart chart = new Chart();

I can import the settings without any problem and the Chart renders to the page without any problem. Why would this make a difference when importing the settings? Apparently this is the problem I have been chasing since you pointed out my error with GetBuffer.

Thank you.

 

WindowsApplication1.zip

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...