tags:

views:

18

answers:

1

Background / Goal

More or less, I'm trying to create a nested group of objects that will be serialized to XML.

The basic hierarchy will be something like:

  • Documents
  • Document
  • Contents

Which will translate to something like (Still this is vary basic)

<Documents>
 <Document>
  <Contents>
   Custom objects
  </Contents>
 </Document>
 <Document>
  <Contents>
    Custom objects
  </Contents>
 </Document>
</Documents>

Because these objects can't be serialized natively, they must implement the IXmlSerializable interface (In the complete version each level contains other objects that require a facade to be serialized, dictionary types and so on, also serializing private variables).

Approach

My approach has been as follows

  1. Serialize the Documents object
  2. Documents object will serialize all it's child document objects
  3. Those child document objects will serialize the content objects
  4. And so on...

Problem

IXmlSerializable provides an XmlReader and XmlWriter to serialize and de-serialize objects. How do I append serialized XML to an XmlWriter? I don't want to write out an element, I want to write out a block of XML.

In other words, my content objects will write out ElementStrings

From Child Element

public void WriteXml(XmlWriter writer) {

    writer.WriteElementString("Type", m_sType);
    writer.WriteElementString("Assembly", m_sAssembly);
    writer.WriteElementString("XSL", m_sXSL);
    writer.WriteElementString("ID", m_sID);
}

But my Document Objects will need to write out the serialized Child objects

public void WriteXml(XmlWriter writer)
{
    //Fake CODE 
    writer.Append(ChildObject.Serialize());
}

This part I can't seem to figure out.

If I append the XML via writer.WriteElementString("childObject", ChildObject.Serialize()); then my XML is escaped (< become & lt; and so on).

This can't happen because while the XML will be used to share objects across systems, it will also be formatted via XSLT and displayed.

Is my approach wrong?

Is there a way to prevent escaping or a method I missed?

Anything along these lines would be very appreciated.

+2  A: 
public void WriteXml(XmlWriter writer)
{
    writer.WriteRaw(ChildObject.Serialize());
}
btlog
Fantastic! Thanks for the quick response!
Brandon Boone