473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
DataContractSerializer, 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="Hello" groupType="World">...</Group>

The closest I can get is the following (which is not what I want):
<Group>
<groupName>Hello</groupName>
<groupType>World</groupType>
<Group>

Is there a way to get the first one, serializing attributes instead of
elements? I can't find anything on the DataMemberAttribute 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.

May 16 '07 #1
2 8309
I don't believe that you can...

In that scenario, perhaps use XmlSerialer instead... but note that
DataContractSerializer 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 DataContractSerializer also respects
IXmlSerializable - but again this would need special handling.

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

Marc
May 16 '07 #2
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 DataContractSerializer 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.

Oooooooooooooooooh yea, that's what i'm talkin about!

Thanks for the help Marc.

- ryan.

On May 16, 5:24 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
I don't believe that you can...

In that scenario, perhaps use XmlSerialer instead... but note that
DataContractSerializer 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 DataContractSerializer also respects
IXmlSerializable - but again this would need special handling.

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

Marc

May 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
2
by: Novice | last post by:
Hi all, I have to decide on an XML structure going forward. The structure is going to house a large amount of data. In the past I've always just used the philosophy of "when in doubt use...
7
by: grwalker | last post by:
I have some classes that have the <Serializable()> attribute applied, which of course by default serializes the class properties as elements. What I would like to do is to be able to override this...
0
by: jts2077 | last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as: <store ...
2
by: Ken Fine | last post by:
In code, I'm adding javascript attributes to form elements on an ASP.NET page: body.Attributes.Add("onClick", "highlight(event);"); body.Attributes.Add("onKeyUp", "highlight(event);");...
6
by: Jakub.Bednarczuk | last post by:
Hallo everybody I have the problem with getting attributes values and also attributes names. I am reading an xml file with DOM. Lets see an example: file I read <root> <Def></Def>...
10
by: Andy Fish | last post by:
Hi, I would like to include extra "hidden" information in a generated HTML page that can be used by javascript functions. I realise that most browsers seem to ignore any tags and attributes...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
0
by: David Lozzi | last post by:
Howdy, I just had a small explosion in my brain, and now i'm a little more confused than I was before. I'm fairly new to XML, but understand it for the most part in regards to formatting. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.