XML
  Home arrow XML arrow Page 2 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 64
    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# - Attributes and Document Complexity


    (Page 2 of 5 )

    When you write your XML document using elements only, your document size will be larger and more difficult to read than writing it using attributes. Let's consider the following XML Document:


    <?xml version="1.0"? >
    <Consultants>
     
    <Consultant>
      
    <name>Michael Youssef</name>
       
    <position>XML Consultant</position>
        
    <age>21</age>
     
    </Consultant>
     
    <Consultant>
      
    <name>Prakhar Deva</name>
       
    <position>.NET Consultant</position>
        
    <age>23</age>
     
    </Consultant>
    </Consultants>

    We can rewrite this XML document better by using attributes.


    <?xml version="1.0"? >
    <Consultants>
     
    <Consultant position="Microsoft.NET Consultant" age="21">Michael Youssef</Consultant>
     
    <Consultant position=".NET Developer" age="23">Prakhar Deva</Consultant>
    </Consultants>

    Now our document is much more readable and efficient (5 lines instead of 13).

    Until now, I've told you about why you should use attributes. Now, let me tell you also why you should use elements.

    Use elements when you need a complex structure.

    When you build a complex structure you have to consider using elements. Consider the following invalid XML document:


    < ? xml version="1.0" ? >
     
    <members>
       
    <member  phone number="0020123658513" phone number="3331684">Michael Youssef</member>
     
    </members>

    Here each member has two phone numbers.  This is invalid because you can't have two attributes with the same name in one element. It will be better logically if you rewrite these phone number attributes as elements so you can know for certain that this person has 2 phone numbers. Let's rewrite the document again.


    < ? xml version="1.0" ? >
     
    <members>
       
    <member name="Michael Youssef">
        
    <phoneNumber>0020123658513</phoneNumber>
        
    <phoneNumber>3331684</phoneNumber>
       
    </member>
     
    </members>

    It's up to you (your experience and the situation) to use elements or attributes. I've shown you some advantages of using both so make sure that you understand this article. But if you have a complex structure to build then definitely you need to use elements. You could also gain some useful information by extracting some data from a SQL Server 2000 database into XML documents and see how the documents are structured.

    Now let's go to our second subject, encoding.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT