Sign in to follow this  

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

This topic is 2178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[CODE]
[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; }
}
[/CODE]

[font=Courier New]I would like to achieve this:[/font]
[CODE]
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">
[/CODE]

But C# gives me this:
[CODE]
<COLLADA version="1.4.1" xmlns="http://www.collada.org/2005/11/COLLADASchema">
[/CODE]

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

Here is my save code:
[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);
}
[/CODE]

Thank you in advance!

Share this post


Link to post
Share on other sites
Hidden
does

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


Work?

Share this post


Link to post
[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this