Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization of object to xml and string without \r\n special characters

I want to serialize my object to xml and then to a string.

  public class MyObject
  {
    [XmlElement]
    public string Name
    [XmlElement]
    public string Location;
  }

I want to obtain a single line string which will lok like this:

<MyObject><Name>Vladimir</Name><Location>Moskov</Location></MyObject>

I am using such code:

  XmlWriterSettings settings = new XmlWriterSettings();
  settings.OmitXmlDeclaration = true;
  settings.Indent = true;
  StringWriter StringWriter = new StringWriter();
  StringWriter.NewLine = ""; //tried to change it but without effect
  XmlWriter writer = XmlWriter.Create(StringWriter, settings);
  XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
  namespaces.Add(string.Empty, string.Empty);
  XmlSerializer MySerializer= new XmlSerializer(typeof(MyObject ));
  MyObject myObject = new MyObject { Name = "Vladimir", Location = "Moskov" };

  MySerializer.Serialize(writer, myObject, namespaces);
  string s = StringWriter.ToString();

This is the closest what I get:

<MyObject>\r\n  <Name>Vladimir</Name>\r\n  <Location>Moskov</Location>\r\n</MyObject>

I do know that I could remove "\r\n" from the string afterwards. But I would like to not produce them at all rather than removing them later.

Thanks for your time.

like image 784
Wodzu Avatar asked Oct 25 '10 11:10

Wodzu


People also ask

What is the correct way of using XML serialization?

XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.

Which class should be used to serialize an object in XML format?

Xml. Serialization namespace) class is used to serialize and deserialize. The class method Serialize is called. Since we have to serialize in a file, we create a " TextWriter ".

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.


2 Answers

You could try:

settings.NewLineHandling = NewLineHandling.None;
settings.Indent = false;

which for me, gives:

<MyObject><Name>Vladimir</Name><Location>Moskov</Location></MyObject>
like image 146
Marc Gravell Avatar answered Sep 21 '22 15:09

Marc Gravell


I used the input above, and here is a generic object to XML string method to be re-used anywhere:

public static string ObjectToXmlString(object _object)
{
    string xmlStr = string.Empty;

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = false;
    settings.OmitXmlDeclaration = true;
    settings.NewLineChars = string.Empty;
    settings.NewLineHandling = NewLineHandling.None;

    using (StringWriter stringWriter = new StringWriter())
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
        {
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, string.Empty);

            XmlSerializer serializer = new XmlSerializer(_object.GetType());
            serializer.Serialize(xmlWriter, _object, namespaces);

            xmlStr = stringWriter.ToString();
            xmlWriter.Close();
        }

        stringWriter.Close();
    }

    return xmlStr;
}
like image 38
Anders Avatar answered Sep 19 '22 15:09

Anders