473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 5900
"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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.