Hello. Serialize the next class in XML

[Serializable] [XmlRoot(Namespace="", ElementName="request")] public class Request { [XmlAttribute("guid")] public Guid Id; [XmlText] public string Body; [XmlIgnore] public DateTime? TimeResponce; } 

I do object serialization

 Request request = new Request(); request.Id = Guid.NewGuid(); request.Body = "<goods><good name=\"Товар 1\"/><good name=\"Товар2\"/></goods>"; var serializer = new XmlSerializer(typeof(Request)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); XmlWriterSettings settings = new XmlWriterSettings() { Indent = false, OmitXmlDeclaration = true, DoNotEscapeUriAttributes = true, }; StringWriter writer = new StringWriter(); using (XmlWriter xw = XmlWriter.Create(writer, settings)) { serializer.Serialize(xw, request, ns); } string value = writer.ToString(); 

At the output, I get a string where the Body field is serialized so that all <and> characters are replaced with the corresponding escape sequences lt; and gt; .

How to serialize an object so that the Body property is serialized without replacing characters into an escape sequence, that is, perceived it as PlainText?

Thank.

  • You want a strange, this should not want. Tell your real task. Why do you want to get invalid XML? (Or valid, but for a completely different object?) - VladD

2 answers 2

At the output, I get a string where the Body field is serialized so that all <and> characters are replaced with the corresponding escape sequences lt; and gt;

It should be so. Xml will be invalid without this.
What prevented you? I hope you don’t get regular values ​​out of it then?

The only way to put the xml text as is is to use CDATA.
Judging by Google, only with the help of attributes to force serialization as CDATA is impossible, you must write custom serialization.


Hmm .. Seems understood. You already have an object serialized to xml, and you want to put it inside another object as a property, but without getting it from the string.

Then only custom serialization.

     [Serializable] [XmlRoot(Namespace = "", ElementName = "request")] public class Request { [XmlAttribute("guid")] public Guid Id; [XmlElement("Body")] public XmlElement BodyElement; [XmlIgnore] public string Body { get { return this.BodyElement.InnerXml; } set { XmlDocument doc = new XmlDocument(); doc.LoadXml("<goods><good name=\"Товар 1\"/><good name=\"Товар2\"/></goods>"); this.BodyElement = doc.DocumentElement; } } [XmlIgnore] public DateTime? TimeResponce; } 

    ....

     Request request = new Request(); request.Id = Guid.NewGuid(); XmlDocument doc = new XmlDocument(); request.Body = "<goods><good name=\"Товар 1\"/><good name=\"Товар2\"/></goods>"; var serializer = new XmlSerializer(typeof(Request)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); XmlWriterSettings settings = new XmlWriterSettings() { Indent = false, OmitXmlDeclaration = true, DoNotEscapeUriAttributes = true, }; StringWriter writer = new StringWriter(); using (XmlWriter xw = XmlWriter.Create(writer, settings)) { serializer.Serialize(xw, request, ns); } string value = writer.ToString(); Console.WriteLine(value); StringReader reader = new StringReader(value); var deserialized = (Request)serializer.Deserialize(reader); Console.WriteLine(deserialized.Body); 

    Or, if desired, leave Request.Body as a simple field / property and transfer parsing to get / set for Request.BodyElement .

    • Thanks, I will try. - Alexcei Shmakov