3

I have a class that I am serializing using System.Xaml.XamlObjectReader and the System.Xaml.XamlXmlWriter. Everything goes good on everything until I get to my property that contains an XDocument. I get Unable to serialize type 'System.Xml.Linq.XDeclaration'.

How can I during my while loop to serialize the object nodes to disk get it to write out the xml in this property?

HatSoft
  • 11,077
  • 3
  • 28
  • 43
twreid
  • 1,453
  • 2
  • 22
  • 42
  • Do you need the entire document? Try serializing just the root XElement. – John Saunders Aug 02 '12 at 19:31
  • Yes, it is a template for a web.config that we ship with our product and I need to save it with the rest of the settings so they can use the whole file with our program to apply all those settings. – twreid Aug 02 '12 at 20:23
  • Yes, but do you need the ` – John Saunders Aug 02 '12 at 21:28
  • No I just need the tags and all the children under it. – twreid Aug 07 '12 at 13:10
  • I just tried to serialize the root and it fails because XElement has no default constructor. Which is the same reason XDocument fails because it contains properties that do not have a default constructor. – twreid Aug 07 '12 at 13:24

1 Answers1

1

You can convert this to CData and serialize it. You can refer this example for details How do you serialize a string as CDATA using XmlSerializer?

Or you can also escape xml tags of ToString() of XDocument. e.g. String escape into XML

Community
  • 1
  • 1
Ankush
  • 2,454
  • 2
  • 21
  • 27
  • I'm not at a computer right now, but this will work with the xamlwriter? – twreid Aug 02 '12 at 20:25
  • I basically took what you said only I converted to a string first. All the XML escaping is ugly, but it works thanks. – twreid Aug 07 '12 at 15:57
  • You should also be able to serialize the root using XML Serialization, since XElement implements IXmlSerializable. XDocument does not. – John Saunders Aug 07 '12 at 16:20