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.
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.
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 ".
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.
You could try:
settings.NewLineHandling = NewLineHandling.None;
settings.Indent = false;
which for me, gives:
<MyObject><Name>Vladimir</Name><Location>Moskov</Location></MyObject>
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With