Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Samurai Jack Jan 30, 2012 at 10:43 AM 2 replies 6.2k views
Original Post
Samurai Jack
Samurai Jack
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!
Nikster
Nikster
[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]
Samurai Jack
Samurai Jack
The namespace is acctualy ignored or screwed when put in the XmlRoot. Every element gets this: q1:elementname.
It' driving me nuts.
Nikster
Nikster
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.