Dessus

I am using serialization in c# 3.0 (trying to serialize). I want to use the schemaLocation xml attribute as that is what is in an example i need to stick to. I cannot work out what c# attribute i need to use to do this.

I have tried:

Code Snippet

public Class exampleClass
{
[System.Xml.Serialization.XmlAttributeAttribute("schemaLocation", Namespace = "http://www.rewerse.net/I1/2006/R2ML http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd")]
public string schemaLocation;
}



where "exampleClass" is the root node in the serialized document.

The output should be like:

Code Snippet

<exampleClass schemaLocation="http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd">
...
</exampleClass>


what comes out is more like:

Code Snippet

<exampleClass xmlns="http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd">
...
</exampleClass>







Re: XML and the .NET Framework schemaLocation problem with Serialization.

Derek Smyth

Hi mate,

You don't need to specify your namespace in your XmlAttribute... try this instead...

Code Snippet

public Class exampleClass
{
[System.Xml.Serialization.XmlAttributeAttribute("schemaLocation")]
public string schemaLocation = "http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd";

}






Re: XML and the .NET Framework schemaLocation problem with Serialization.

Martin Honnen

Usually the schemaLocation attribute belongs into the namespace http://www.w3.org/2001/XMLSchema-instance and has the prefix xsi. You can achieve that as follows:

Code Snippet

public class exampleClass

{

public exampleClass() { }

[System.Xml.Serialization.XmlAttributeAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]

public string schemaLocation = "http://www.rewerse.net/I1/2006/R2ML http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd";

}

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

exampleClass example = new exampleClass();

XmlSerializer serializer = new XmlSerializer(typeof(exampleClass));

serializer.Serialize(Console.Out, example, namespaces);