DataContractSerializer: xml elements vs. attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cranley

    DataContractSerializer: xml elements vs. attributes

    I've spent hours tonight trying to figure out how I can get my WCF
    DataContract to serialize properly and I'm going nuts. The
    DataContractSer ializer, at this point, seems to me to be very
    restrictive with regards to the final xml format. For example, from
    what I can see, it only serializes XML elements... but what if I want
    the following:

    <Group groupName="Hell o" groupType="Worl d">...</Group>

    The closest I can get is the following (which is not what I want):
    <Group>
    <groupName>Hell o</groupName>
    <groupType>Worl d</groupType>
    <Group>

    Is there a way to get the first one, serializing attributes instead of
    elements? I can't find anything on the DataMemberAttri bute attribute
    that gives an option to serializing as an element or an attribute, and
    I haven't been able to find any documentation anywhere discussing this
    possibility.

    Thanks.

  • Marc Gravell

    #2
    Re: DataContractSer ializer: xml elements vs. attributes

    I don't believe that you can...

    In that scenario, perhaps use XmlSerialer instead... but note that
    DataContractSer ializer will happily use this (or equivalent)
    internally if you remove remove the data-contract attributes and add
    in the [Serializable] and [XmlAttribute] etc markers. Of course, this
    changes behaviour in a number of ways...

    Finally, note that DataContractSer ializer also respects
    IXmlSerializabl e - but again this would need special handling.

    If you want simple metadata-based classes, then just live with what
    DataContractSer ializer gives you. Or perhaps run it through an xslt?

    Marc


    Comment

    • ryan baldwin

      #3
      Re: DataContractSer ializer: xml elements vs. attributes

      Yea, i went to bed on this last night and, as it happens so often, in
      my half-sleep state I thought of an alternative solution. I shouldn't
      care how the DataContractSer ializer serializes the datacontract...
      don't care at all. If I want to be that anal about preserving the
      original states of the XML that I'm writing a DataContract class for
      to send over the wire for a WCF service, then I'll simply write 2
      services: 1 that returns my DataContract (which serializes as
      whatever), and a second that returns the raw XML as a string.

      Ooooooooooooooo ooh yea, that's what i'm talkin about!

      Thanks for the help Marc.

      - ryan.

      On May 16, 5:24 am, "Marc Gravell" <marc.grav...@g mail.comwrote:
      I don't believe that you can...
      >
      In that scenario, perhaps use XmlSerialer instead... but note that
      DataContractSer ializer will happily use this (or equivalent)
      internally if you remove remove the data-contract attributes and add
      in the [Serializable] and [XmlAttribute] etc markers. Of course, this
      changes behaviour in a number of ways...
      >
      Finally, note that DataContractSer ializer also respects
      IXmlSerializabl e - but again this would need special handling.
      >
      If you want simple metadata-based classes, then just live with what
      DataContractSer ializer gives you. Or perhaps run it through an xslt?
      >
      Marc

      Comment

      Working...