473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ 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.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>

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 5923
"Bill Geake" <ge******@hotma il.com> wrote in message news:12******** *************** ***@posting.goo gle.com...
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 XmlElementAttri butes to the properties of subThing belonging
to this second namespace. XmlElementAttri butes allow you to
customize the ElementName, Namespace (URI), and other
properties of how the property/field gets serialized.

public class subClassDataTyp e
{
[XmlElement( Namespace="some thingelse")]
public string subThingElement 1;

[XmlElement( Namespace="some thingelse")]
public string subThingElement 2;
}

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 XmlSerializerNa mespaces 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(t ypeof(testClass ));

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

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

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

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

// Serialize the instance, c, using the XmlSerializerNa mespaces.
mySerializer.Se rialize( 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
2299
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 !) ! Unfortunately, there's a problem in it, which prevents me to Serialize and Validate an XML file In XML Schema files, there's the same namespace, but the value for
0
903
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"; subClassDataType s = new subClassDataType(); s.subThingElement1 = "data3";
0
1708
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). ---------------------------------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <xs:schema id="TaskRequest" targetNamespace="http://xyz.com/TaskResponse/1.0" elementFormDefault="qualified" xmlns="http://xyz.com/TaskResponse/1.0"
1
4365
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 done this before but this time I have no idea why I am getting the error. Any ideas ?? THANKS ! <qcsttatus> <manual sourcerootfolder="" manualname="">
0
1722
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" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="FAAAvxType"> <xs:sequence> <xs:choice>
5
1713
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 signature has changed so I added: Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) End Sub to my new class so that I can specify custom deserialization however it does
6
6056
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 problem is that in my derived classes, the XML that is automatically generated is lacking the properties of the base class. For example: public class MyBaseClass { public MyBaseClass ( ) { } private string myVariable;
0
1809
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 /> <LOB /> <Name> <FirstName />
2
3208
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 installalled to use it as a default settings file. Works fine on machines having the same culture (en-US) settings. But goes wrong when the object is serialized on a machine with the culture set to english and and deserialized on another machine with...
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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 we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.