0

I have a custom type UserSettingConfig I want to save in my database, I want to save it as pure XML as the type might be changed later and migrating pure xml is easier than a binary objet.

    public class Serialize
{
    private readonly DataContractSerializer _serializer;

    public Serialize()
    {
        _serializer = new DataContractSerializer(typeof(UserSettingConfig));
    }

    public string SerializeObject(UserSettingConfig userSettingConfig)
    {
        using (var memoryStream = new MemoryStream())
        {
            _serializer.WriteObject(memoryStream, userSettingConfig);

            string userSettingXml = memoryStream.ToString();

            memoryStream.Close();

            return userSettingXml;
        }
    }

    public UserSettingConfig DeSerializeObject(string userSettingXml)
    {
        UserSettingConfig userSettingConfig;

        using (var stream = new MemoryStream(userSettingXml))
        {
            stream.Position = 0;
            userSettingConfig = (UserSettingConfig)_serializer.ReadObject(stream);
        }

        return userSettingConfig;
    }
}

This dont work as the Memory Stream want a byte array or int

I want my Serialize to return a string (I can save as varchar(MAX) in my database)

3 Answers 3

3

DataContractSerializer.WriteObject has an overload that takes an XmlWriter. You can construct one of those that writes the XML to a StringBuilder:

private static string SerializeToString(object objectToSerialize)
{
  var serializer = new DataContractSerializer(objectToSerialize.GetType());
  var output = new StringBuilder();
  var xmlWriter = XmlWriter.Create(output);

  serializer.WriteObject(xmlWriter, objectToSerialize);
  xmlWriter.Close();

  return output.ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You may also consider serializing to JSON instead of XML, using the excellent JSON.NET library which can serialize even the most complex objects easily. JSON is very compact and is still readable.

To serialize:

string json =  Newtonsoft.Json.JavaScriptConvert.SerializeObject(anySerializableObject);

To deserialize:

MyClass instance = (MyClass) Newtonsoft.Json.JavaScriptConvert.DeserializeObject(json, typeof(MyClass));

2 Comments

This is not an open source project and there is a licens for closed source :/
JSON.NET is licensed using the MIT License. According to Wikipedia, "[...] proprietary software retains its proprietary nature even though it incorporates software under the MIT License". en.wikipedia.org/wiki/MIT_License
0

If you need xml without xml declaration, you should use XmlWriterSettings. For instance when you need xml string for node but not entire xml document.

private static string SerializeToString(object objectToSerialize)
{
  var serializer = new DataContractSerializer(objectToSerialize.GetType());
  var output = new StringBuilder();
  var xmlWriter = XmlWriter.Create(output,  new XmlWriterSettings() { OmitXmlDeclaration = true});

  serializer.WriteObject(xmlWriter, objectToSerialize);
  xmlWriter.Close();

  return output.ToString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.