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 !!