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

Home Posts Topics Members FAQ

Serialization and elementFormDefault

Jay
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
'elementFormDefault' is'nt the same ! Sometimes it is qualified,
sometimes not !

***********************
Root.xsd
--------
<xsd:schema targetNamespace="URI1" xmlns="URI1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
***********************
Child.xsd
---------
<xsd:schema targetNamespace="URI1" xmlns="URI1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
***********************
Then I have an XML instance like this :

***********************
RootInstance.xml
----------------
<n:RootElement xmlns:n="URI1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="URI1
Root.xsd">
<ChildElement>
<n:Name>TOTO</n:Name>
<n:BirthDate>1960-12-31</n:BirthDate>
<n:Country>France</n:Country>
</ChildElement>
</n:RootElement>
***********************
So I have two types of Element : those with prefix "n:", those with no
prefix, but both refers to the same namespace URI1.

Now I want to serialize Classes that have the same structure. I
declare them like that :

***********************
[XmlRoot(Namespace = "URI1",
ElementName = "RootElement")]
public class RootElement
{
[XmlElement("ChildElement")]
public ChildElement childElement;

public RootElement()
{}
}
[XmlRoot("ChildElement")]
public class ChildElement
{
[XmlElement("Name")]
public string name;

[XmlElement("BirthDate")]
public string birthDate;

[XmlElement("Country")]
public string country;

public ChildElement()
{}
}
*************************
In order to Serialize, here's the main program's code :

*************************

StreamWriter _writer = null;

RootElement _root = new RootElement();

try
{
XmlSerializer _serConfig = new XmlSerializer(typeof(RootElement));

XmlSerializerNamespaces _xsn = new XmlSerializerNamespaces();
_xsn.Add("n", "http://www.hprim.org/hprimXML");
_xsn.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

string nomFichier = @"c:\SerializedXML.xml";

_writer = new StreamWriter(nomFichier,false);
ChildElement _child = new ChildElement();

_child.name = "TOTO";
_child.birthDate = "1960-12-31";
_child.country = "France";
_root.childElement = _child;

_serConfig.Serialize(_writer, _hprim, _xsn);

}
catch (System.Exception _err)
{
System.Diagnostics.EventLog.WriteEntry("Serializer ",_err.Message,
EventLogEntryType.Error);
}
finally
{
if (_writer!=null) _writer.Close();
}

***********************
But when i run the main program I get that :

***********************
SerializedXML.xml
----------------
<n:RootElement xmlns:n="URI1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="URI1
Root.xsd">
<n:ChildElement>
<n:Name>TOTO</n:Name>
<n:BirthDate>1960-12-31</n:BirthDate>
<n:Country>France</n:Country>
</n:ChildElement>
</n:RootElement>
***********************
I think my result is logic, because ChildElement takes the same
namespace as RootElement and also the same prefix. But I want it not
have this prefix.
I think the question is : how can i define a different behavior for
ChildElement without changing its namespace ??

Thanx a lot for that !!
Nov 12 '05 #1
2 2278
Jay
Finally I found it !
The message took a long time to appear so that i could find the answer

ChildElement has the same namespace as RootElement
But you can specify in the Attribute [XMLElement("ChilElement")] the
value of a property called "Form" and define it as "Unqualified"

[XMLElement("ChilElement", Form = XmlSchemaForm.Unqualified)]

you have to use System.Xml.Schema
Nov 12 '05 #2
* Jay wrote in microsoft.public.dotnet.xml:
<n:RootElement xmlns:n="URI1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="URI1
Root.xsd">
<ChildElement>
<n:Name>TOTO</n:Name>
<n:BirthDate>1960-12-31</n:BirthDate>
<n:Country>France</n:Country>
</ChildElement>
</n:RootElement>
***********************

So I have two types of Element : those with prefix "n:", those with no
prefix, but both refers to the same namespace URI1.


No, they do not, ChildElement is in no namespace here, only the default
namespace is inherited which is in your example no namespace. Maybe you
want

<n:RootElement
xmlns:n = "URI1"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "URI1 Root.xsd">
<ChildElement xmlns = "URI1">
...

but then I miss the difference? If you want ChildElement to be in no
namespace as in your instance above, you must not declare a namespace
for it.
Nov 12 '05 #3

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

Similar topics

0
by: vector | last post by:
I've found that I have a use for XML serialization. Does .NET's support for XML serialization of classes require that a new document be created each time a class is serialized, or is there some...
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). ...
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"...
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...
3
by: dickster | last post by:
I have a query surrounding about elementFormDefault I have abbreviated the schemas - and hopefully have included all the relevant information I get the following error message...
5
by: daz_oldham | last post by:
Hi everyone I am a new poster to this group, so hello to you all! Having just started a new job I have been thrown in the deep end with some heavy XML work so I am finding my feet and I am...
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...
1
by: Bardo | last post by:
Hi all, From what I can read, it doesn't look like I'm the only one getting confused with the elementFormDefault and attributeFormDefault properties of XML schemas. I am wanting to get a full...
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: 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,...
1
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...
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: 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.