I am successfully serializing to XML from a class like this:
private static void CreateXML()
{
testClass c = new testClass();
c.stringElement = "data1";
c.stringElement2 = "data2";
subClassDataType s = new subClassDataType();
s.subThingElement1 = "data3";
s.subThingElement2 = "data4";
c.subThing = s;
XmlSerializer mySerializer = new XmlSerializer(typeof(testClass));
System.IO.StreamWriter myWriter = new
System.IO.StreamWriter("test3.xml");
mySerializer.Serialize(myWriter, c);
}
My class definitions are as follows:
[Serializable]
[System.Xml.Serialization.XmlRootAttribute("testCla ss",
Namespace="urn:test")]
public class testClass
{
public string stringElement;
public string stringElement2;
public subClassDataType subThing;
}
public class subClassDataType
{
public string subThingElement1;
public string subThingElement2;
}
And here are the results:
<?xml version="1.0" encoding="utf-8"?>
<testClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:test">
<stringElement>data1</stringElement>
<stringElement2>data2</stringElement2>
<subThing>
<subThingElement1>data3</subThingElement1>
<subThingElement2>data4</subThingElement2>
</subThing>
</testClass>
However, for my real world application, subThing is defined in another
namespace. When I am done, my resulting file needs to look like this:
<?xml version="1.0" encoding="utf-8"?>
<testClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:test"
xmlns:ns2="somethingelse">
<stringElement>data1</stringElement>
<stringElement2>data2</stringElement2>
<subThing>
<ns2:subThingElement1>data3</subThingElement1>
<ns2:subThingElement2>data4</subThingElement2>
</subThing>
</testClass>
Note the additional namespace declaration in the root element, along
with the qualified names in subThing. This is relatively easy when
creating an XML Document from scratch with the CreateAttribute()
method, however since I am serializing from a class, how can I
accomplish this? THANK YOU in advance for any help!
-Bill