Jump to content
  • Advertisement
Sign in to follow this  
Samurai Jack

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

This topic is 2510 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:

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

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!