I'm fairly new to C#, so maybe this is simple, but I couldn't find any answer to this anywhere. I am trying to write something that will take an XML document of unknown structure and wrap it in a mesage wrapper and send it to another program. I figured it was easiest to just use the XML serialization stuff to do this, but I can't figure out how to get this to work with the XML document.
This is a simplified version of what I am trying to do:
[XmlRoot]
public class MessageWrapper
{
private string info;
private object message;
[XmlElement]
public string Information { get { return info; } set { info = value; } }
[XmlElement]
public object Message { get { return message; } set { message = value; } }
}
public class User
{
[STAThread]
static void Main()
{
MessageWrapper wrapper = new MessageWrapper();
wrapper.Information = "Additional information about message";
String xmlStringOfUnknownStructure = "<SomeTag>some text</SomeTag><AnotherTag>more text</AnotherTag>";
// What can I do with xmlStringOfUnknownStructure here that would allow me to set it to
// wrapper.Message and it serialize properly
XmlSerializer serializer = new XmlSerializer(typeof(MessageWrapper));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, request);
writer.Close();
}
}
So I want the result to look like this:
<MessageWrapper> <Information>Additional information about message</Information> <Message> <SomeTag>some text</SomeTag> <AnotherTag>more text</AnotherTag> </Message> </MessageWrapper>
So is there any good way to do this, or should I not be trying to use the Serialization here.

Serializing an XML document
Chris Hannon
You can also create XmlWriter by XmlWriter.Create() and write out XML without filling out any intermediate representation.
This will be simpler.
XmlSerialization will be helpful when you already use classes that you serializing in other places of your program. XmlSerialization gives you read/write for your classes "for free".
ibeanz
If you are getting exceptions please paste the full exception here so everybody can see. Thanks.
0oseven
I figured it out. If you break the MessageWrapper into two classes it will work. Here is the three classes that will do what I want
[XmlRoot] public class MessageWrapper { private string info; private object message; [XmlElement] public string Information { get { return info; } set { info = value; } } [XmlElement] public Message Message { get { return message; } set { message = value; } } } public class Message : IXmlSerializable { private string contents; public string Contents { get { return contents; } set { contents = value; } } public System.Xml.Schema.XmlSchema GetSchema() { throw new Exception("The method or operation is not implemented."); } public void ReadXml(System.Xml.XmlReader reader) { throw new Exception("The method or operation is not implemented."); } public void WriteXml(System.Xml.XmlWriter writer) { // This method will write an XML string as is to an XmlWriter writer.WriteRaw(contents); } } public class User { [STAThread] static void Main() { MessageWrapper wrapper = new MessageWrapper(); wrapper.Information = "Additional information about message"; String xmlStringOfUnknownStructure = "<SomeTag>some text</SomeTag><AnotherTag>more text</AnotherTag>"; Message message = new Message(); message.Contents = xmlStringOfUnknownStructure; wrapper.Message = message; XmlSerializer serializer = new XmlSerializer(typeof(MessageWrapper)); StringWriter writer = new StringWriter(); serializer.Serialize(writer, request); writer.Close(); } }