4

I have a class that I want to serialise to XML. The name of outer element of the class when serialised needs to be controlled by the application.

At design time I know the element name can be controlled by the use an XmlTypeAttribute

 [XmlElement(Name="MyName")]

I need to control this at run time so this will not work for me.

I also looked at IXmlSerializable to create my own serialisation code, but again this will not work as this only allows control of the 'internals' of the class and not the external wrapper.

Are there any other options available?

4
  • 1
    You may be able to define a static variable "ElementName" on your class and then do [XmlElement(MyClass.ElementName)]. I didn't post this as an answer since I'm not sure it will work correctly. Commented May 15, 2011 at 21:34
  • You cannot specify an attribute argument at runtime. Commented May 15, 2011 at 21:38
  • 2
    You can use XmlAttributeOverrides msdn.microsoft.com/en-us/library/… at runtime, but only if you control the use of the XmlSerializer. It won't work if you use your custom type with something that implicitly serializes - like WCF for example. If you post more of your scenario I can offer some better insight as to whether it would work for you. Commented May 15, 2011 at 21:39
  • @SLaks - not quite true. You can use XmlAttributeOverrides. It is designed for this specific purpose. Commented May 15, 2011 at 21:40

1 Answer 1

2

Yes, as Cheeso pointed out in the comments you can do this using XmlAttributeOverrides

XmlAttributes overrideAttributes = new XmlAttributes();
overrideAttributes.XmlRoot = new XmlRootAttribute("Testing");

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
overrides.Add(typeof(string[]), overrideAttributes);

XmlSerializer serialise = new XmlSerializer(typeof(string[]), overrides);

using (MemoryStream stream = new MemoryStream())
{
    serialise.Serialize(stream, new string[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() });
}

Output:

<?xml version="1.0"?>
<Testing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string>37d47837-62d0-46dc-9747-709b91bdac6e</string>
  <string>9cd904a9-f86f-46c1-a2aa-49c44bc3c654</string>
</Testing>

Xml serialisation (roughly) works on the basis that:

  • The object being serialised decides how its contents are serialised (including attributes of the root element)
  • However the caller is responsible for creating the root element (and consuming it on deserialisation).

You can see this from the way that the IXmlSerializable interface works - The object being serialised can use the XmlRootAttribute attribute as a suggestion to the caller on what the root element should look like, however ultimately it is up to the caller to create the root element (normally hanled by the XmlSerializer class).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.