I have two objects and I want to merge them:
public class Foo
{
public string Name { get; set; }
}
public class Bar
{
public Guid Id { get; set; }
p
Another Option:
Modify the first class like so
public class Foo
{
public string Name { get; set; }
[System.Xml.Serialization.XmlAnyElementAttribute()]
public XmlElement Any {get;set;}
}
Take the second class and serialize it into an XmlElement like so:
XmlElement SerializeToElement(Type t, object obj)
{
XmlSerializer ser = new XmlSerializer(t);
StringWriter sw = new StringWriter();
using (XmlWriter writer = XmlWriter.Create(sw, settings))
ser.Serialize(writer, obj);
string val = sw.ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
return (XmlElement)doc.DocumentElement;
}
Set property Any to the XmlElement, and serialize it You'll see the XML for the other class embedded in the document, complete with all the namespaces
You can even get away with this if the class was generated using Xsd.exe if you use the following as an element:
<xs:any namespace="##any" processContents="lax" />
I believe you can also get away with an array of XmlElements for more than one sub-class.
Deserializaing should be a matter of inspecting the XmlElements and then looking for a matching class, or perhaps using the namespace to find the class.
This is a lot tidier than messing about with string manipulation.