Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : Serialization and elementFormDefault


jerome.avoustin NO[at]SPAM insa-lyon.fr
7/21/2004 6:13:56 AM
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 ??

jerome.avoustin NO[at]SPAM insa-lyon.fr
7/21/2004 11:15:58 AM
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)]

Bjoern Hoehrmann
7/21/2004 10:40:22 PM
* Jay wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

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
AddThis Social Bookmark Button