xml serialization of a class and adding qualified namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Geake

    xml serialization of a class and adding qualified namespace

    I am successfully serializing to XML from a class like this:

    private static void CreateXML()
    {
    testClass c = new testClass();
    c.stringElement = "data1";
    c.stringElement 2 = "data2";

    subClassDataTyp e s = new subClassDataTyp e();
    s.subThingEleme nt1 = "data3";
    s.subThingEleme nt2 = "data4";

    c.subThing = s;

    XmlSerializer mySerializer = new XmlSerializer(t ypeof(testClass ));
    System.IO.Strea mWriter myWriter = new
    System.IO.Strea mWriter("test3. xml");
    mySerializer.Se rialize(myWrite r, c);
    }

    My class definitions are as follows:

    [Serializable]
    [System.Xml.Seri alization.XmlRo otAttribute("te stClass",
    Namespace="urn: test")]
    public class testClass
    {
    public string stringElement;
    public string stringElement2;
    public subClassDataTyp e subThing;
    }

    public class subClassDataTyp e
    {
    public string subThingElement 1;
    public string subThingElement 2;
    }

    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>
    <subThingElemen t1>data3</subThingElement 1>
    <subThingElemen t2>data4</subThingElement 2>
    </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="some thingelse">
    <stringElement> data1</stringElement>
    <stringElement2 >data2</stringElement2>
    <subThing>
    <ns2:subThingEl ement1>data3</subThingElement 1>
    <ns2:subThingEl ement2>data4</subThingElement 2>
    </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
Working...