Knowledge Base Nr: 00290 objtoxml.cs - http://www.swe-kaiser.de
Downloads:
c#: convert any serializable object to XML-string
public string SerializeAsString(Object src)
{
string text = "";
System.IO.MemoryStream stream = null;
StreamReader tr = null;
try
{
stream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(src.GetType());
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("fx", "http://www.asam.net/xml/fbx");
xsn.Add("flexray", "http://www.asam.net/xml/fbx/flexray");
xsn.Add("lin", "http://www.asam.net/xml/fbx/lin");
xsn.Add("can", "http://www.asam.net/xml/fbx/can");
xsn.Add("ho", "http://www.asam.net/xml");
xsn.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xsn.Add("xs", "http://www.asam.net/xml/fbx/all http://www.asam.net/xml/fbx/1_2_0a/xsd/fibex4multiplatform.xsd");
serializer.Serialize(stream, src, xsn);
tr = new StreamReader(stream);
stream.Seek(0, SeekOrigin.Begin);
String line;
while ((line = tr.ReadLine()) != null)
text += line + "\n";
stream.Close();
tr.Close();
}
catch (Exception /*ex*/)
{
if (stream != null)
stream.Close();
if (tr != null)
tr.Close();
}
return text;
}