XML
  Home arrow XML arrow XML Tricks for C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XML Tricks for C#
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 65
    2004-03-24

    Table of Contents:
  • XML Tricks for C#
  • Attributes and Document Complexity
  • A First Look at Encoding
  • Unicode
  • Encoding with XML

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    1. The sub-element <name> to store the name.
    2. 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.

    More XML Articles
    More By Michael Youssef


     

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek