locked
How to Serialize an object to XML in memory and get the XML string RRS feed

  • Question

  • Hi,
       How can i serialize an object in memory and get the XML string without writing to an file and then reading from it as is done below. All i want is to get the XML string after serialization in memory without writing to a file on disk.

     

    DataContractSerializer dataContractserializer = new DataContractSerializer(typeof(DataContractObjectClass));

    FileStream writer = new FileStream(@"C:\XML\SourceXml.xml", FileMode.Create);

    dataContractserializer.WriteObject(writer, this._dataContract);

    writer.Close();

    Thanks
     

    Saturday, February 21, 2009 4:45 AM

Answers

  • Thanks for the reply Henrik..But i found the answer 


    public static string ToXml(Object objToXml,
                         bool includeNameSpace) {
    StreamWriter stWriter=null;
    XmlSerializer xmlSerializer;
    string buffer;
    try {
      xmlSerializer =
            new XmlSerializer(objToXml.GetType());
      MemoryStream memStream = new MemoryStream();
      stWriter = new StreamWriter(memStream);
      if (!includeNameSpace){
     
        System.Xml.Serialization.XmlSerializerNamespaces xs=
                             new XmlSerializerNamespaces();
                            
        //To remove namespace and any other inline
        //information tag                     
        xs.Add("","");
        xmlSerializer.Serialize(stWriter, objToXml,xs);
      }
      else{
       xmlSerializer.Serialize(stWriter, objToXml);
      }
      buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
    }
    catch(Exception Ex){
      throw Ex;
    }
    finally {
      if(stWriter!=null) stWriter.Close();
    }
    return buffer;
    }
    Monday, February 23, 2009 2:13 PM

All replies

  • Have you considered using StringBuilder and xmlWriter?


    Monday, February 23, 2009 8:57 AM
  • You can use MemoryStream instead of FileStream. Then you can read it as string by passing the MemoryStream object to an instance of StreamReader.

    Mohammadreza Jooyandeh http://www.jooyandeh.info/
    Monday, February 23, 2009 2:10 PM
    Moderator
  • Thanks for the reply Henrik..But i found the answer 


    public static string ToXml(Object objToXml,
                         bool includeNameSpace) {
    StreamWriter stWriter=null;
    XmlSerializer xmlSerializer;
    string buffer;
    try {
      xmlSerializer =
            new XmlSerializer(objToXml.GetType());
      MemoryStream memStream = new MemoryStream();
      stWriter = new StreamWriter(memStream);
      if (!includeNameSpace){
     
        System.Xml.Serialization.XmlSerializerNamespaces xs=
                             new XmlSerializerNamespaces();
                            
        //To remove namespace and any other inline
        //information tag                     
        xs.Add("","");
        xmlSerializer.Serialize(stWriter, objToXml,xs);
      }
      else{
       xmlSerializer.Serialize(stWriter, objToXml);
      }
      buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
    }
    catch(Exception Ex){
      throw Ex;
    }
    finally {
      if(stWriter!=null) stWriter.Close();
    }
    return buffer;
    }
    Monday, February 23, 2009 2:13 PM
  • Hey.. good post!

    but what I have to do with the StreamWriter when I have a web service - and no "direct" XML-file!?

    Wednesday, October 31, 2012 10:08 AM