How can you control .NET DataContract serialization so it uses XML attributes instead of elements?
Asked Answered
D

3

19

If I have a class marked as a DataContract and a few properties on it marked with DataMember attributes I can serialize it out to XML easily, but it would create output like:

<Person>
    <Name>John Smith</Name>
    <Email>[email protected]</Email>
    <Phone>123-123-1234</Phone>
</Person>

What I would prefer is attributes, like...

<Person Name="John Smith" Email="[email protected]" Phone="123-123-1234" />

The DataMember attribute allows me to control the Name and Order but not whether it is serialized as an element or attribute. I have looked around and found DataContractFormat and IXmlSerializable but I am hoping there is there an easier solution.

What is the easiest way to do this?

Dys answered 26/2, 2009 at 18:47 Comment(1)
Also, I need the XML to work this way while JSON continues to work.Dys
W
17

You can't do this with the DataContractSerializer; if you want attributes you need to use the XmlSerializer instead. With the DataContractSerializer class a more restrictive subset of the XML specification is permitted which improves performance, and improves the interoperability of published services, but gives you rather less control over the XML format.

If you're using WCF services then take a look at XmlSerializerFormatAttribute which allows you to use the XmlSerializer for serialization.

Wolframite answered 26/2, 2009 at 18:50 Comment(4)
There has to be a simple way to make it use attributes. I do not care about web services, I just to output the XML in a specific format.Dys
Hi, I', afraid Greg is correct - see msdn.microsoft.com/en-us/library/ms731923.aspx for the limitations of the DataContractSerializer.Garbage
If you don't care about web services, then why are you using WCF? Are you just trying to serialize some classes? If so, then use the XmlSerializer by itself.Legra
So strange how WCF claims all this enterop capabilities and it can't do this. So many schemas for services online require attributes in xml. I understand performance, but let me choose so i can talk to other services.Criss
E
40

You can do this with the DataContractSerializer - the answer is to take over the Xml serialization yourself by implementing the IXmlSerializable interface. For write-only support - you can leave the implementation of ReadXml empty, and return null for GetSchema, and then write the implementation of WriteXml as follows:

public class MyPerson : IXmlSerializable
{
  public string Name { get; set;}
  public string Email { get; set;}
  public string Phone { get; set;}

  public XmlSchema GetSchema() { return null; }
  public void ReadXml(XmlReader reader) { }
  public void WriteXml(XmlWriter writer)
  { 
    writer.WriteAttributeString("name", Name);
    writer.WriteAttributeString("email", Email);
    writer.WriteAttributeString("phone", Phone);
  } 
}

If you're using the same type for, say, JSON serialization as well, then you are still free to add the DataContract and DataMember attributes - the DataContractSerializer will utilise the IXmlSerializable interface implementation only when writing Xml.

I blogged about this here.

Ebbarta answered 17/8, 2009 at 11:25 Comment(6)
Are you certain that the DataContractSerializer will invoke the WriteXml method of this code?Legra
I know - it seems unlikely - but I've just posted an article on our corporate blog at labs.jobserve.com/Articles.aspx/… where you can download source code that shows it in action (using it to implement OpenSearch suggestions). The clue came from this MSDN content: msdn.microsoft.com/en-us/library/ms731923.aspx.Ebbarta
Yes, thanks for that, it seems that the correct implementation should use the XmlSchemaProviderAttribute to be considered properly implemented. It's incredible the amount of gotchas there are with this - but I have to say that the most basic solution has worked wonders for our code. At least this means that you don't necessarily have to rely upon XmlSerializer!Ebbarta
@AndrasZoltan DataContractJsonSerializer will use the IXmlSerializable interface implementationHoyt
Cool - in the modern era we're more likely to be using JSON.Net, though.Ebbarta
This also works fine for reading, I just had to set the XmlRoot attribute. Example: to parse <SOAP:fault><detail><jpr:Fault xmlns:jpr="http://com.sap/aii/proxy/xiruntime/"> I used [XmlRoot(ElementName = "Fault", Namespace = "http://com.sap/aii/proxy/xiruntime/")] public class Fault : IXmlSerializableSuffer
W
17

You can't do this with the DataContractSerializer; if you want attributes you need to use the XmlSerializer instead. With the DataContractSerializer class a more restrictive subset of the XML specification is permitted which improves performance, and improves the interoperability of published services, but gives you rather less control over the XML format.

If you're using WCF services then take a look at XmlSerializerFormatAttribute which allows you to use the XmlSerializer for serialization.

Wolframite answered 26/2, 2009 at 18:50 Comment(4)
There has to be a simple way to make it use attributes. I do not care about web services, I just to output the XML in a specific format.Dys
Hi, I', afraid Greg is correct - see msdn.microsoft.com/en-us/library/ms731923.aspx for the limitations of the DataContractSerializer.Garbage
If you don't care about web services, then why are you using WCF? Are you just trying to serialize some classes? If so, then use the XmlSerializer by itself.Legra
So strange how WCF claims all this enterop capabilities and it can't do this. So many schemas for services online require attributes in xml. I understand performance, but let me choose so i can talk to other services.Criss
J
0

You could convert back and forth between attributes and elements when serializing/deserializing. The following works for the latter.

    private XmlReader AttributesToElements( Stream stream )
    {
            var root = XElement.Load( stream );
            foreach ( var element in root.Descendants() ) {
                    foreach ( var attribute in element.Attributes() )
                            element.Add( new XElement( root.Name.Namespace + attribute.Name.LocalName, (string)attribute ) );
                    element.Attributes().Remove();
            }
            return root.CreateReader();
    }
Jasik answered 20/5, 2012 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.