1

When I'm deserializing a xml string, I need to save a XElement outerXml on a string property called prop2.

My XML:

<MyObj>
  <prop1>something</prop1>
  <prop2>
    <RSAKeyValue>
      <Modulus>...</Modulus>
      <Exponent>...</Exponent>
    </RSAKeyValue>
  </prop2>
  <prop3></prop3>
</MyObj>

My object:

public class MyObj
{
    [XmlElement("prop1")]
    public string prop1 { get; set; }

    [XmlText]
    public string prop2 { get; set; }

    [XmlElement(ElementName = "prop3", IsNullable = true)]
    public string prop3 { get; set; }
}

I'm deserializing using XmlSerializer, like this:

var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(new StringReader(myXmlString));

I tried to use [XmlText] to save XML text in prop2 But I'm only getting null.

What I need to do to save <RSAKeyValue><Modulus>...</Modulus><Exponent>...</Exponent></RSAKeyValue> like text in prop2?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Ninita
  • 1,209
  • 2
  • 19
  • 45
  • Possible duplicate of [How to Deserialize XML document](http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) – MethodMan Nov 16 '15 at 17:31
  • @MethodMan I don't see where there is the duplication... In that question they need to deserialize each xml element to a property, but I need to deserialize all RSAKeyValue outer xml to a single property as text string – Ninita Nov 16 '15 at 17:38
  • @MethodMan believe me I already did since this morning. But my question is a particular problem. – Ninita Nov 16 '15 at 17:41
  • Can't you just have custom class to read this properties (would be easier to do it that way and if you still need string - create extra non-serialized property to read it) – Alexei Levenkov Nov 16 '15 at 17:52
  • @AlexeiLevenkov I can but `[XmlText]` shouldn't do the trick? Because then I need to use `prop2` as string, always. – Ninita Nov 16 '15 at 17:56

1 Answers1

4

XmlText will give value as XML encoded as text ("&gt;prop2&lt;...") see XmlTextAttribute

By default, the XmlSerializer serializes a class member as an XML element. However, if you apply the XmlTextAttribute to a member, the XmlSerializer translates its value into XML text. This means that the value is encoded into the content of an XML element.

One possible solution - use XmlNode as type of the property:

public class MyObj
{
    [XmlElement("prop1")]
    public string prop1 { get; set; }

    public XmlNode prop2 { get; set; }

    [XmlElement(ElementName = "prop3", IsNullable = true)]
    public string prop3 { get; set; }
}

var r = (MyObj)serializer.Deserialize(new StringReader(myXmlString));
Console.WriteLine(r.prop2.OuterXml);

Alternatively you may make whole object implement custom Xml serialization or have custom type that matches XML (to read normally) and have additional property to represent that object as XML string.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    I think he'd want the OutterXml rather than the InnerXml: Inner: ...... Outer: ...... – merthsoft Nov 16 '15 at 18:23
  • And if I use `public XmlNode prop2 { get; set; }` I get the exception `There was an error reflecting property 'prop2'` – Ninita Nov 16 '15 at 18:43
  • Try just `[XmlElement] public XmlNode prop2 { get; set; }` – merthsoft Nov 16 '15 at 18:46
  • Still not working, I'm getting `The specified type is abstract: name='XNode', namespace='', at .` exception – Ninita Nov 16 '15 at 18:53
  • That's strange, @Ninita. [This](http://paste.ee/p/qzNfK) is my full code, and it works as expected. Can you paste what you have? – merthsoft Nov 16 '15 at 18:59
  • 1
    @merthsoft now I deleted some namespaces like `System.Linq`, `System.Runtime.Serialization`and `System.Xml.Linq` and now it works! – Ninita Nov 16 '15 at 19:14
  • 1
    @Ninita - just noticed that I have not removed `[XmlText]` from my sample which indeed would cause problem you originally got with my code (updated). Using just `[XmlElement]` would work too. – Alexei Levenkov Nov 16 '15 at 20:32
  • @AlexeiLevenkov Yes, I saw that. I saw [your code](http://paste.ee/p/qzNfK) and is equal to my, the only difference are the file namespaces, I had more. I just removed some of those namespaces and now it works. – Ninita Nov 17 '15 at 08:54