Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can .NET XmlSerializer class deserialize InnerXml as a string?

I have a very specific deserialization need, see example below:

say I have following class:

[Serializable]
public class Person {
public string Name { get; set; }
public string PersonXml { get; set; }
}

and following XML

<Person>
  <Name>John</Name>
  <PersonXml><someXmlFragment>text</someXmlFragment></PersonXml>
</Person>

What I want is the XmlSerializer class to deserialize InnerXml of the <PersonXml> element to the PersonXml property as a string. I'm wondering if it can be done.

NOTE: I know I can encode the content of <PersonXml> escaping illegal XML chars, but I would prefer to leave the inner XML more human friendly (not containing &lt; and other entities that will only cofuse my end user)

like image 909
Piotr Owsiak Avatar asked Jul 22 '09 15:07

Piotr Owsiak


People also ask

How does the XmlSerializer work C#?

The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.

Is XmlSerializer thread safe?

Since XmlSerializer is one of the few thread safe classes in the framework you really only need a single instance of each serializer even in a multithreaded application.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

Why do we use XmlSerializer class?

XmlSerializer enables you to control how objects are encoded into XML. The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors.


1 Answers

You can always implement IXmlSerializable and do whatever you fancy through XmlReader.

like image 160
Anton Tykhyy Avatar answered Dec 08 '22 10:12

Anton Tykhyy