How to stream an object as XML to a string
private static string getXMLAsAString(LoadTenderTrip t) { XmlSerializer x = new XmlSerializer(typeof(LoadTenderTrip)); StringWriter w = new StringWriter(); try { x.Serialize(w,t); } finally { w.Close(); } return w.ToString(); }
Controlling root and root field name
[System.Xml.Serialization.XmlRootAttribute(ElementName="MY_XML_CLASS", Namespace="", IsNullable=false)] Public class MyClass { }
Will result in
<MY_XML_CLASS> </MY_XML_CLASS>
With out "ElementName" it will be
<MyClass> </MClass>
Controlling array elements
[System.Xml.Serialization.XmlElementAttribute("LOAD_TENDER_EQUIPMENT")] public LoadTenderEquipment[] equipmentArray;
Without the above attribute you will see
<someroot> <equipmentArray> <LoadTenderEquipment> </LoadTenderEquipment> </equipmentArray> </someroot>
With the above attribute you will see
<someroot> <LOAD_TENDER_EQUIPMENT> </LOAD_TENDER_EQUIPMENT> </someroot>
How to write a string to a file
public static void writeStringToFile(string filename,string msg) { StreamWriter w = null; try { w = new StreamWriter(filename); w.WriteLine(msg); } finally { if (w!= null) w.Close(); } }
satya - Wednesday, June 13, 2007 10:06:09 AM
More sample code
//************************************************
//* packages and imports
//************************************************
using System;
using System.Data;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
namespace XMLSerializationTest
{
class DBTest
{
[STAThread]
static void Main(string[] args)
{
//Read the xml from a file into an object
//Serialize the xml back to output stream
folder f = xmlToCode();
System.Console.WriteLine(
"XML to code to print stream");
printObjectAsXML(f);
//code to xml
f = codeToXML();
System.Console.WriteLine(
"Code to XML to print stream");
printObjectAsXML(f);
}//eof-main
//************************************************
//* xml to code
//************************************************
static folder xmlToCode()
{
FileStream fs = null;
try
{
fs = new System.IO.FileStream("sample.xml",
System.IO.FileMode.Open);
//returns the folder object
XmlSerializer s =
new XmlSerializer(typeof(folder));
folder o = (folder)s.Deserialize(fs);
return o;
}
finally
{
if (fs != null)
{
fs.Close();
}
}
}
//************************************************
//* code to xml
//************************************************
static folder codeToXML()
{
//Create a folder
//id is 1, name is folder1,
// and holds two items
folder folder = new folder("1","folder1",2);
//dotnet uses a typed array to bind to xml
//children. This is a nuisance as you need
//to know the length of the array to
//populate it before hand. In addition it
//is the programmer's responsibility
//to initialize the array
file file1 = new file("1","file1");
file1.contents="file1 conent";
file file2 = new file("1","file2");
file2.contents="file2 contents";
folder.files[0]=file1;
folder.files[1] = file2;
return folder;
}
//************************************************
//* print support
//************************************************
static void printObjectAsXML(Object obj)
{
XmlSerializer x =
new XmlSerializer(obj.GetType());
x.Serialize(System.Console.Out,obj);
}
}//eof-class
}//eof-namespace
satya - Wednesday, June 13, 2007 10:33:19 AM
dotnet what are xml serialization attributes collections
Search for: dotnet what are xml serialization attributes collections
satya - Wednesday, June 13, 2007 10:39:58 AM
Attributes that control xml serialization
satya - Wednesday, June 13, 2007 10:41:58 AM
an example of arrays or collections
[XmlArrayItem(typeof(Manager)),
XmlArrayItem(typeof(Employee))]
public Employee[] Employees;
satya - Wednesday, June 13, 2007 10:52:53 AM
Or you can do this as well
[XmlArrayItem(typeof(Manager)),
XmlArrayItem(typeof(Employee))]
public ArrayList Employees;
satya - Wednesday, June 13, 2007 11:03:36 AM
another sample definition of classes
public class ColumnSpec
{
public String name;
public String expression;
public ColumnSpec() { }
public ColumnSpec(String inName, String inExpression)
{
name = inName;
expression = inExpression;
}
}//eof-class
public class CQSpec
{
[XmlArrayItem(typeof(ColumnSpec))]
public ArrayList columnSpecList = new ArrayList();
public void addColumnSpec(String name, String expression)
{
columnSpecList.Add(new ColumnSpec(name, expression));
}
}//eof-class
satya - Wednesday, June 13, 2007 11:04:25 AM
exercising objects
public String getCQSpecAsXML()
{
CQSpec cqs = this.getCQSpec();
XmlSerializer xs = new XmlSerializer(typeof(CQSpec));
StringWriter sw = new StringWriter();
xs.Serialize(sw, cqs);
return sw.ToString();
}
public CQSpec getCQSpecFromXML(String xmlSpec)
{
XmlSerializer xs = new XmlSerializer(typeof(CQSpec));
StringReader sr = new StringReader(xmlSpec);
return (CQSpec)xs.Deserialize(sr);
}