473,406 Members | 2,220 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,406 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>
</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
Nov 12 '05 #1
0 763

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 !) !...
1
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"...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
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...

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.