Sunday, January 30, 2011

XSD to byte array and vice versa


need to construct your own byte array from a defiined XSD ? heres how

 
first construct the class from XSD (or XML)

we use XSD tool
  • go to Visual studio command prompt
  • Go to path of xsd file(I.E c:\xsd)
  • xsd [file name].xsd /c /n:[namespace]
  • where
    • /c – generate classes
    • /n - namespace

Now you have created the class representation of the XML (or XSD) file.
you can now use this 2 functions to serialize and deserialize the objects , to byte array and from byte array
public static byte[] ObjectToXmlBytes(object o)

{

MemoryStream memoryStream = new MemoryStream();

XmlSerializer serializer = new XmlSerializer(o.GetType());

serializer.Serialize(memoryStream, o);

return memoryStream.ToArray();



}



public static object XmlBytesToObject(byte[] bytes, Type t)

{

MemoryStream memoryStream = new MemoryStream(bytes);

memoryStream.Position = 0;

XmlSerializer serializer = new XmlSerializer(t);

return serializer.Deserialize(memoryStream);

}
enjoy

0 תגובות:

Post a Comment

 
Home | About | Link | Link
Simple Proff Blogger Template Created By Herro | Inspiring By Busy Bee Woo Themes