Serialize
public static string SerializeObject(T obj)
{
try
{
string xmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);
xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
ASCIIEncoding encoding = new ASCIIEncoding();
xmlString = encoding.GetString(memoryStream.ToArray());
return xmlString;
}
catch
{
return string.Empty;
}
}
Deserialize
public static T DeserializeObject(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byteArray = encoding.GetBytes(xml);
MemoryStream memoryStream = new MemoryStream(byteArray);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);
return (T)xs.Deserialize(memoryStream);
}
Object Classes
// the root element in the resulting xml, there can be only one
[XmlRoot("adf")]
public class ADF
{
public ADF()
{
Prospect = new Prospect();
}
[XmlElement("prospect")]
public Prospect Prospect;
}
// a wrapper element for all the other elements in the document, not the same as the root element
public class Prospect
{
public Prospect()
{
Vehicles = new Vehicles();
}
[XmlElement("requestdate")]
public string RequestDate;
[XmlElement("vehicles")]
public Vehicles Vehicles;
[XmlElement("customer")]
public Customer Customer;
}
public class Customer
{
public Customer()
{
Name = new Name();
}
[XmlElement("name")]
public Name Name;
}
// an element with text inside and an attribute called "part"
public class Name
{
[XmlText]
public string Text;
[XmlAttribute("part")]
public string Part;
}
// an element that contains a list of child elements
[Serializable] // notice this is necessary for using generic collections (using System.Collections.Generic)
public class Vehicles
{
[XmlElement("vehicle")]
public List<Vehicle> Vehicle;
}
// a child element with many properties
public class Vehicle
{
[XmlElement("year")]
public string Year;
[XmlElement("make")]
public string Make;
[XmlElement("model")]
public string Model;
}
Resulting XML (or Source XML)
<adf>
<prospect>
<customer>
<name part="full">Patrick Lewis</name>
</customer>
<vehicles>
<vehicle>
<year>2005</year>
<make>Toyota</make>
<model>Tacoma</model>
</vehicle>
<vehicle>
<year>2007</year>
<make>Ducati</make>
<model>Monster</model>
</vehicle>
</vehicles>
</prospect>
</adf>
Working with the object
public void serialize()
{
// create an object, (I didn't use constructors so this is a pita)
ADF adf = new ADF();
adf.Prospect.Customer.Name.Text = "Patrick Lewis";
adf.Prospect.Customer.Name.Part = "full";
Vehicle v1 = new Vehicle();
v1.Make = "Toyota";
v1.Year = "2005";
v1.Model = "Tacoma";
adf.Prospect.Vehicles.Vehicle.Add(v1);
Vehicle v2 = new Vehicle();
v2.Make = "Ducati";
v2.Model = "Monster";
v2.Year = "2007";
adf.Prospect.Vehicles.Vehicle.Add(v2);
// serialize
string xml = SerializeObject(adf);
// use the xml for something
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
// deserialize
ADF adf2 = DeserializeObject(xDoc.OuterXml);
// use the object data for something
string name = adf2.Prospect.Customer.Name.Text;
}
3 comments:
Who hate Microsoft, please don't use WWINDOWS OS
Please......Even if I did hate Windows (which I don't) it is the most widely deployed OS and you will probably have to program for it if you are a proffessional programmer. Can we all just grow up and pull our heads out of the sand already.
Sarcasm is lost on some people.
Post a Comment