[C#] XmlSerializer Why are attributes before namespace?!

Started by
2 comments, last by Nikster 12 years, 2 months ago
Hello!

I'm writing a Collada EXPOTER using serializable classes in conjunction with
the XmlSerializer so please do not post answers about XmlDocument.

Here is my class header:

[Serializable]
[XmlRoot(ElementName = "COLLADA", IsNullable = false)]
public class DaeModel
{
private const string DefaultNamespace = @"http://www.collada.org/2005/11/COLLADASchema";
private string _version = "1.4.1";
[XmlAttributeAttribute(AttributeName = "version", Namespace = DefaultNamespace)]
public string Version
{
get { return _version; }
set { _version = value; }
}


[font=Courier New]I would like to achieve this:[/font]

<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">


But C# gives me this:

<COLLADA version="1.4.1" xmlns="http://www.collada.org/2005/11/COLLADASchema">


Why is the version in front of the namespace?! How can I put the version behind the xmlns?

Here is my save code:

public void Save(string fileName)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.UTF8;
settings.OmitXmlDeclaration = false;
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, DefaultNamespace);
XmlWriter writer = XmlWriter.Create(Console.Out, settings);
XmlSerializer serializer = new XmlSerializer(GetType(), DefaultNamespace);
serializer.Serialize(writer, this, namespaces);
}


Thank you in advance!
Advertisement
does

[XmlRoot(ElementName = "COLLADA", IsNullable = false, Namespace = "http://www.collada.org/2005/11/COLLADASchema")]


Work?
Nikster: how is that related to C# / XmlSerializer? Where should that pe put?
[font=monospace]Have you tried putting the namespace name on the root element rather than the attribute?[/font]

[font=monospace]EDIT:- I removed my previous post because my eyes failed to read most of your post, Monday morning blues :)[/font]
The namespace is acctualy ignored or screwed when put in the XmlRoot. Every element gets this: q1:elementname.
It' driving me nuts.
Hrm, sorry then I've no idea, I've never used other attributes alongside xmlns, however with attributes I wouldn't have thought ordering of attributes to be an issue when parsing the actual data, is this something that's causing a problem with the data you're saving an is being used by something else or is it just an annoyance visually?

XmlSerializer serializer = new XmlSerializer(GetType(), DefaultNamespace);

This is the line that appears to be appending the namespace name itself to the attribute list and there seems to be no way of specifying where it will go.

So sorry I can;t help but maybe someone else knows of somthing.

This topic is closed to new replies.

Advertisement