Write ICON data to an xml file

S

SteveT

I have a structure that contains both 32x32 and 16x16 icons plus some text.
I want to write all this to an XML file so that I can recover the icons later
in an application. Can someone tell me how to properly serialize the
System.Drawing.Icon structure to an XML file?

The following code doesn't write the icon information to the xml file.

private void CreateXmlFile(string filename)
{

string fullPath = String.Format(@"{0}", filename);

XmlWriter xw = null;
try
{
XmlSerializer xSer = new XmlSerializer(typeof(AppIconList));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
xw = XmlWriter.Create(fullPath, settings);
xSer.Serialize(xw, this.List);
}
catch (Exception e)
{
System.Windows.MessageBox.Show(e.Message);
}
finally
{
// Ensure the file is closed.
if (xw != null)
xw.Close();
}
}
 
S

Steven Cheng[MSFT]

Hi Steve,

From your description, you have an .NET appliccation which will use some
Icon file, and you also want to persist them into a XML file so that you
can retrieve them out and reuse next time, correct?

In my opinion, since Icons are actually of binary image stream, you'd
better still persist them through their binary content, however, since the
persistent storage is xml file, you can use Convertion to convert the
binary stream of the Icon from binary to string. For example,you can use
Convert.ToBase64String

#Convert..::.ToBase64String Method
http://msdn2.microsoft.com/en-us/library/system.convert.tobase64string.aspx

To get the binary stream of a Icon object, you can simply call its "Save"
method and provide a "MemoryStream" class, then, you can get the saved
byte[] array of your Icon object and save it into XML file.

#Icon..::.Save Method
http://msdn2.microsoft.com/en-us/library/system.drawing.icon.save.aspx

Next time, you just need to extract the binary blob(convert from base64
string to binary) and use Icon's c onstructor to recreate the Icon. How do
you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
S

SteveT

This will work. Thank you.
--
-----------
Thanks,
Steve


Steven Cheng said:
Hi Steve,

From your description, you have an .NET appliccation which will use some
Icon file, and you also want to persist them into a XML file so that you
can retrieve them out and reuse next time, correct?

In my opinion, since Icons are actually of binary image stream, you'd
better still persist them through their binary content, however, since the
persistent storage is xml file, you can use Convertion to convert the
binary stream of the Icon from binary to string. For example,you can use
Convert.ToBase64String

#Convert..::.ToBase64String Method
http://msdn2.microsoft.com/en-us/library/system.convert.tobase64string.aspx

To get the binary stream of a Icon object, you can simply call its "Save"
method and provide a "MemoryStream" class, then, you can get the saved
byte[] array of your Icon object and save it into XML file.

#Icon..::.Save Method
http://msdn2.microsoft.com/en-us/library/system.drawing.icon.save.aspx

Next time, you just need to extract the binary blob(convert from base64
string to binary) and use Icon's c onstructor to recreate the Icon. How do
you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Write ICON data to an xml file
thread-index: AchYfDHCgoWs1fVGTGKG6m0ECW+YYg==
X-WBNR-Posting-Host: 15.235.249.70
From: =?Utf-8?B?U3RldmVU?= <[email protected]>
Subject: Write ICON data to an xml file
Date: Wed, 16 Jan 2008 12:13:01 -0800

I have a structure that contains both 32x32 and 16x16 icons plus some text.
I want to write all this to an XML file so that I can recover the icons later
in an application. Can someone tell me how to properly serialize the
System.Drawing.Icon structure to an XML file?

The following code doesn't write the icon information to the xml file.

private void CreateXmlFile(string filename)
{

string fullPath = String.Format(@"{0}", filename);

XmlWriter xw = null;
try
{
XmlSerializer xSer = new XmlSerializer(typeof(AppIconList));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
xw = XmlWriter.Create(fullPath, settings);
xSer.Serialize(xw, this.List);
}
catch (Exception e)
{
System.Windows.MessageBox.Show(e.Message);
}
finally
{
// Ensure the file is closed.
if (xw != null)
xw.Close();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top