473,472 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.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>

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
</testClass>
Nov 12 '05 #1
1 5903
"Bill Geake" <ge******@hotmail.com> wrote in message news:12**************************@posting.google.c om...
However, for my real world application, subThing is defined in another
namespace. When I am done, my resulting file needs to look like this: : : 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?


Add XmlElementAttributes to the properties of subThing belonging
to this second namespace. XmlElementAttributes allow you to
customize the ElementName, Namespace (URI), and other
properties of how the property/field gets serialized.

public class subClassDataType
{
[XmlElement( Namespace="somethingelse")]
public string subThingElement1;

[XmlElement( Namespace="somethingelse")]
public string subThingElement2;
}

This produces a document that ought to be equivalent to
your requirements, but if you want to manifest additional
control that guarantees the ns2 prefix is used, it would
be necessary that an XmlSerializerNamespaces gets
created, has the "ns2" prefix (associated of course with the
same Namespace URI you've given to these child elements
of subThing) added to it, and is passed to the XmlSerializer
when you call Serialize( ).

// Create XmlSerializer for your Type.
XmlSerializer mySerializer = new XmlSerializer(typeof(testClass));

// Create an XmlSerializerNamespaces to control the prefixes
// that get generated for namespace URIs appearing in the
// serialized XML.
XmlSerializerNamespaces myNamespaces = new
XmlSerializerNamespaces( );

// Associate ns2 with the namespace URI used for subThing's
// child elements.
myNamespaces.Add( "ns2", "somethingelse");

// When using XmlSerializerNamespaces, you must specify xsi
// and xsd namespace declarations if you want them.
myNamespaces.Add( "xsi", "http://www.w3.org/2001/XMLSchema-instance");
myNamespaces.Add( "xsd", "http://www.w3.org/2001/XMLSchema");

// Create a StreamWriter to write the XML serialization to.
StreamWriter myWriter = new StreamWriter("test3.xml");

// Serialize the instance, c, using the XmlSerializerNamespaces.
mySerializer.Serialize( myWriter, c, myNamespaces);

// Flush and close the StreamWriter to ensure everything gets
// committed to file.
myWriter.Flush( );
myWriter.Close( );
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay | last post by:
Hi ! I've got a big XML Schema defined in several files. There are files in which there are just type definitions. But i can't modify this Schema and i must use it (Don't ask me why !) !...
0
by: Bill Geake | last post by:
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"; ...
0
by: Alberto Grosso Nicolin | last post by:
We have the following XML schema: there's a root element (Response) with of a single child element (Result). ...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
0
by: eSapient | last post by:
I generated serialization/deserialization code for this schema using the xsd tool: <?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFormDefault="qualified"...
5
by: Perecli Manole | last post by:
I have a class that has been serialized and saved to disk. I am trying to deserialize it back into the same class which now has an extra private member. It will not deserialize because its...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
0
by: groovyghoul | last post by:
Hi I have the following XML file: =========================================================== <?xml version="1.0" encoding="UTF-16"?> <Policy xmlns="http://tempuri.org/richard.xsd"> <TransType...
2
by: Nishant Mehta | last post by:
Hi all, I am serialiizing a c# object to an XML file to save some application settings. The idea is to ship this xml file along with the application and deserialize after the application is...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.