Translation Of C# Class to XML And Vice versa
-
Hi All I Have a Class name Product.cs that have attributes or properties like id Name Price etc... Now on run time i have List Of Product Class... Now i have to Translate this List In to Xml Local Client File... and Serialize and deserialize that.. Pls Help Me Thanks In Advance.. Sachin
-
Hi All I Have a Class name Product.cs that have attributes or properties like id Name Price etc... Now on run time i have List Of Product Class... Now i have to Translate this List In to Xml Local Client File... and Serialize and deserialize that.. Pls Help Me Thanks In Advance.. Sachin
Serialisation is binary, not XML, AFAIK. I don't know if you can automatically have it done to XML, or if you need to write your own code.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Serialisation is binary, not XML, AFAIK. I don't know if you can automatically have it done to XML, or if you need to write your own code.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Hello Sir,, I Want to Make a XML File from That Class List locally
-
Hello Sir,, I Want to Make a XML File from That Class List locally
OK, so you need to write a method which takes a stringbuilder, or an XML builder or something, and adds the class instance to the XML document being built. Your solution is to manually write code that creates XML, or reads XML and uses it to create a class instance. Ideally, you want to call a method as you iterate over each class instance, and it builds the XML that way.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
OK, so you need to write a method which takes a stringbuilder, or an XML builder or something, and adds the class instance to the XML document being built. Your solution is to manually write code that creates XML, or reads XML and uses it to create a class instance. Ideally, you want to call a method as you iterate over each class instance, and it builds the XML that way.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Thanks Dude
-
Serialisation is binary, not XML, AFAIK. I don't know if you can automatically have it done to XML, or if you need to write your own code.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hi All I Have a Class name Product.cs that have attributes or properties like id Name Price etc... Now on run time i have List Of Product Class... Now i have to Translate this List In to Xml Local Client File... and Serialize and deserialize that.. Pls Help Me Thanks In Advance.. Sachin
here i have an example for you to serialize and deserialize a class
/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public string SerializeObject ( Object pObject, Type et )
{
try
{
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream ( );
XmlSerializer xs = new XmlSerializer ( et );
XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.Indentation = 4;
xs.Serialize ( xmlTextWriter, pObject );
memoryStream = ( MemoryStream ) xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString ( memoryStream.ToArray ( ) );
return XmlizedString;
}
catch ( Exception e )
{
System.Console.WriteLine ( e );
throw new Exception(e.Message,e);
// return null;
}
}/// <summary> /// Method to reconstruct an Object from XML string /// </summary> /// <param name="pXmlizedString"></param> /// <returns></returns> public Object DeserializeObject ( String pXmlizedString, Type e ) { XmlSerializer xs = new XmlSerializer ( e ); MemoryStream memoryStream = new MemoryStream ( StringToUTF8ByteArray ( pXmlizedString ) ); XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream , Encoding.UTF8 ); return xs.Deserialize ( memoryStream ); }
and here the class how it looks.
\[Serializable()\] \[XmlRoot("Attrib")\] public class AttributeClass { string sIdentifier; string sAlias; \[XmlElement("Identifier")\] public string Identifier { get { return sIdentifier; } set { sIdentifier = value; } } \[XmlElement("Alias")\] public string Alias { get { return sAlias; } set { sAlias = value; } } }
and here the AttributeCollection as Class to serialize it..
\[Serializable()\] \[XmlRoot("AttribsList")\] \[XmlInclude(typeof(AttributeClass)), XmlInclude(typeof(System.Collections.Generic.List<AttributeClass>))\] public class AttribsList :
-
here i have an example for you to serialize and deserialize a class
/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public string SerializeObject ( Object pObject, Type et )
{
try
{
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream ( );
XmlSerializer xs = new XmlSerializer ( et );
XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.Indentation = 4;
xs.Serialize ( xmlTextWriter, pObject );
memoryStream = ( MemoryStream ) xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString ( memoryStream.ToArray ( ) );
return XmlizedString;
}
catch ( Exception e )
{
System.Console.WriteLine ( e );
throw new Exception(e.Message,e);
// return null;
}
}/// <summary> /// Method to reconstruct an Object from XML string /// </summary> /// <param name="pXmlizedString"></param> /// <returns></returns> public Object DeserializeObject ( String pXmlizedString, Type e ) { XmlSerializer xs = new XmlSerializer ( e ); MemoryStream memoryStream = new MemoryStream ( StringToUTF8ByteArray ( pXmlizedString ) ); XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream , Encoding.UTF8 ); return xs.Deserialize ( memoryStream ); }
and here the class how it looks.
\[Serializable()\] \[XmlRoot("Attrib")\] public class AttributeClass { string sIdentifier; string sAlias; \[XmlElement("Identifier")\] public string Identifier { get { return sIdentifier; } set { sIdentifier = value; } } \[XmlElement("Alias")\] public string Alias { get { return sAlias; } set { sAlias = value; } } }
and here the AttributeCollection as Class to serialize it..
\[Serializable()\] \[XmlRoot("AttribsList")\] \[XmlInclude(typeof(AttributeClass)), XmlInclude(typeof(System.Collections.Generic.List<AttributeClass>))\] public class AttribsList :
Thanks A Lot Brother!!! Cheers