Use XmlSerializer to serialize/deserialize class from disk or other. You
can include code like below for your class. Your class should contain
public fields/properties for the fields you want serialized.
// In the declaration section. My class here is XmlReply, yours is what
ever you want.
private static XmlSerializer ser = new XmlSerializer(t ypeof(XmlReply) );
// Methods in your class
public string ToXmlString()
{
byte[] bytes = ToBytes(false);
return Encoding.UTF8.G etString(bytes) ;
}
public static XmlReply FromXmlString(s tring xmlString)
{
if ( xmlString == null )
throw new ArgumentNullExc eption("xmlStri ng");
XmlReply xr = null;
using (StringReader sr = new StringReader(xm lString))
{
xr = (XmlReply)ser.D eserialize(sr);
return xr;
}
}
/// <summary>
/// Serialize class to utf-8 encoded byte array. Serialize defaults to
/// using UTF8 so we can avoid an additional XmlWriter stream.
/// </summary>
/// <param name="prefixLen "></param>
/// <returns></returns>
public byte[] ToBytes(bool prefixLen)
{
using(MemoryStr eam ms = new MemoryStream())
{
if ( prefixLen )
ms.Position = 4;
ser.Serialize(m s, this, NS);
if ( prefixLen )
{
if ( ms.Length > uint.MaxValue )
throw new ArgumentExcepti on("Serialized class bigger then
uint.MaxValue") ;
uint len = (uint)ms.Length ;
byte[] lenBytes = BitConverter.Ge tBytes(len);
ms.Position = 0;
ms.Write(lenByt es, 0, lenBytes.Length );
}
return ms.ToArray();
}
}
/// <summary>
/// Deserializes byte array to XmlRequest. Assumes not length prefix.
/// </summary>
/// <param name="ba"></param>
/// <returns></returns>
public static XmlReply FromBytes(byte[] ba)
{
XmlReply xr = null;
MemoryStream ms = new MemoryStream(ba );
xr = (XmlReply)ser.D eserialize(ms);
return xr;
}
--
William Stacey, MVP
http://mvp.support.microsoft.com
"SP" <EA*@sp.com> wrote in message
news:u#******** ******@TK2MSFTN GP09.phx.gbl...
Lowell,
I have to store hierarchical data which app.config does not support. So I
am using a .xml file for that job. I cant use your suggested method.
Thanks,
SP
"Lowell Heddings" <lo****@mindjun ction.com> wrote in message
news:O9******** *****@TK2MSFTNG P10.phx.gbl... You can use the System.Configur ation.Configura tionSettings.Ap pSettings
to store the settings in a file.
Lowell
SP wrote: Please advice. I am not familiar with the concepts of windows service.
I am trying to access a xml configuration file from windows service.
But it is failing. configuration file is in the same directory from where I install windows service. I dont want to give an absolute path to the location
of file. When I try to start windows service, it is expecting the XML
file to be in c:\windows\syst em32\.
Please let me know whether there is a way to access xml files from windows service.
Thanks,
SP