XML Tricks for C#
(Page 1 of 5 )
In this article, gain knowledge about the difference between elements and attributes in XML, as well as differences in character sets. The author shows the benefits and drawbacks of using XML components and why you should carefully consider your character set when developing your software.
I find that C# programmers who start their careers with .NET, and have little experience with win32 platform or Classic ASP, struggle with XML and its related technologies. So if you are one of them, I advise you to spend some time with XML- it is the future. Let's talk about elements vs. attributes.
Should I Be Using Elements or Attributes?
This is a question every developer asks when working with XML. This was one of the biggest topics of debate in the world of XML. Attributes or Elements? I can tell you much of the time they are quite interchangeable, but sometimes it is better to choose one over the other. Here are a few topics that you must think about each time you ask yourself the elements vs. attributes question.
Which is Easier to Work With?
When you use Attributes in your XML Documents you will notice that they are easier to manipulate and to work with. Here's an example:
<university>
<school>Information Technology</school>
</university>
Here I used the element <school> inside the <university> element. With elements, you must open the element tag, then write your data (PCDATA), then close the tag--which is time-consuming. Imagine that your XML document contains 100 elements; there will be a lot of closing tags for your elements. Let's rewrite this fragment using Attributes
<university school="Information Technology"></university>
It's now easier because we don't have the school element with its opening and closing tags. You could even make it better like this:
<university school="Information Technology" />
As you know, university is an empty element (because there is not data between its opening and closing tags) so we can write it like that and again save text so our XML document will look simpler.
Attributes are Strongly Validated by DTDs.
This is an advantage of attributes over elements. DTDs strongly validate attributes over elements, because elements can contain either PCDATA (any text character data) or sub-elements. As such, there's no need for validation. With attributes you can declare either CDATA types (character data), an ID type, an Entity, an option from an enumeration list, or other types.
Use Attributes to Describe Elements' behavior.
The best use of attributes is for determination of the element behavior. In other words, you can use attributes to specify metadata about your data (about your elements). Consider the following example:
<person>
<name>Michael Youssef</name>
</person>
This fragment describes a person.
<person position="XML Consultant">
<name>Michael Youssef</name>
</person>
Now the attribute position provides us with data (metadata) about our data (here I mean the element <person>). When I was discussing the above fragment with one developer from Microsoft, he said that the <name> sub-element also stores the metadata because it stores the name of that <person> element.
Note that I used two different methods to store the metadata about the person element:
- The sub-element <name> to store the name.
- An attribute to store the position of the person element.
But we can write the above code using only attributes:
<person name="Michael Youssef" position="XML Consultant"> </person>
And again a better writing for that code as following:
<person name="Michael Youssef" position="XML Consultant" />
Note that it's up to you to decide the formula about your meta data. You can use whatever you find better suits the solution you are working on. So using attributes to store metadata is more efficient than using elements, unless you have complex data to store and retrieve.
Next: Attributes and Document Complexity >>
More XML Articles
More By Michael Youssef