PDA

Click to See Complete Forum and Search --> : XML best practice - elemenst or attributes


Merrion
Jul 9th, 2009, 10:40 AM
Howdy

When persisting recors to XML what is the best practice regarding whether a field should be persisted as an Attribute or an Element? I've kind of settled on having my key fields as attributes but is that good practice, bad practice or neither?

techgnome
Jul 9th, 2009, 11:39 AM
I've done it both ways... often, it's like anything else - it depends on what works. There's "purists" in both camps. I'm on the fence. I tend to want to put things in to attributes if it's something like a property of that element. Unless it's longer text or text that may have special characters, in which case I'll use elements.

Having Key items as attributes makes sense, it's data that uniquely identifies the item. I don't know if it's best practice or bad, but I'd say it's probably common practice.

-tg

DeanMc
Jul 11th, 2009, 06:47 AM
My rule is that it is an element unless it is an attribute of that element :) like so


<Person>
<Details AreUpToDate="yes">
<PhoneNumber IsActive="No">0871234567</PhoneNumber>
</Details>
</Person>

Nightwalker83
Jul 13th, 2009, 01:29 AM
My rule is that it is an element unless it is an attribute of that element :) like so


I take it the attributes are bits in red? Can the attributes value be changed pragmatically without manually editing it?

Merrion
Jul 13th, 2009, 03:41 AM
My rule is that it is an element unless it is an attribute of that element :) like so


<Person>
<Details AreUpToDate="yes">
<PhoneNumber IsActive="No">0871234567</PhoneNumber>
</Details>
</Person>


OK - but if you turn that into a class are both PhoneNumber and IsActive properties? or is IsActive a method?

DeanMc
Jul 13th, 2009, 07:01 AM
@Nightwalker, of course... In LINQ its very easy to do.

@Merrion, well they would all be properties if they need to be referenced outside the class. The key is which data container to use. Since Phone number contains more than one piece of data it becomes complex and therefore needs to either be a class or structure depending on how complex it it and how you wish to use it.

penagate
Jul 15th, 2009, 05:03 AM
When to use elements versus attributes (http://www.ibm.com/developerworks/xml/library/x-eleatt.html)