Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Translation Of C# Class to XML And Vice versa

Translation Of C# Class to XML And Vice versa

Scheduled Pinned Locked Moved C#
csharpxmlhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin Dubey
    wrote on last edited by
    #1

    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

    C F 2 Replies Last reply
    0
    • S Sachin Dubey

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      S 0 2 Replies Last reply
      0
      • C Christian Graus

        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.

        S Offline
        S Offline
        Sachin Dubey
        wrote on last edited by
        #3

        Hello Sir,, I Want to Make a XML File from That Class List locally

        C 1 Reply Last reply
        0
        • S Sachin Dubey

          Hello Sir,, I Want to Make a XML File from That Class List locally

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • C Christian Graus

            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.

            S Offline
            S Offline
            Sachin Dubey
            wrote on last edited by
            #5

            Thanks Dude

            1 Reply Last reply
            0
            • C Christian Graus

              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.

              0 Offline
              0 Offline
              0x3c0
              wrote on last edited by
              #6

              You don't necessarily have to write your own code to manually serialise a class. The XmlSerializer class can handle most of the work.

              OSDev :)

              1 Reply Last reply
              0
              • S Sachin Dubey

                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

                F Offline
                F Offline
                freakyit
                wrote on last edited by
                #7

                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 :
                
                S 1 Reply Last reply
                0
                • F freakyit

                  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 :
                  
                  S Offline
                  S Offline
                  Sachin Dubey
                  wrote on last edited by
                  #8

                  Thanks A Lot Brother!!! Cheers

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups