在asp.net中使用xml文件的两种类型及用法

示例数据均来自MSDN,由于第一次接触XML,很多东西都不太明白,只有看MSDN了。
        要在网页上显示XML的数据,通常情况下用的多的是加载到dropdownlist控件中做一个选项(当用XML文件来存储同一类型的信息时,如我用来存储数据库中的表名及其中文名字),或者直接做为一个table数据源,此时一般可用于gridview及其它类似控件的数据源。
        众所周知,一般情况下我们存储在数据库中的表的名字都用英文或者中文拼音等等,总之为英文字母,那么要让用户知道这个表,或者要选择这个表来进行操作时,如查询这个表,编辑这个表。那么首先就要知道表的名字。当然,我们不能把英文字母的表名让用户来选择,所以我才想到用XML文件来存储这个转换信息。内容很简单,一个属性是数据库中的表的名字,一个属性是中文表名。
        在通常使用的XML文件中存储的数据有两种方式(不知道还有没有别的类型,刚接触了解的不多:):
示例1:
<?xml version="1.0" standalone="yes"?>
<bookstore>
    
<book ISBN="10-000000-001" 
        title
="The Iliad and The Odyssey" 
        price
="12.95">
    
<comments>
        
<userComment rating="4" 
            comment
="Best translation I've read." />
        
<userComment rating="2" 
            comment
="I like other versions better." />
      
</comments>
   
</book>
   
<book ISBN="10-000000-999" 
        title
="Anthology of World Literature" 
        price
="24.95">
   
<comments>
      
<userComment rating="3" 
          comment
="Needs more modern literature." />
      
<userComment rating="4" 
          comment
="Excellent overview of world literature." />
   
</comments>
   
</book>
    
<book ISBN="11-000000-002" 
        title
="Computer Dictionary" 
        price
="24.95" >
      
<comments>
         
<userComment rating="3" 
             comment
="A valuable resource." />
      
</comments>
   
</book>
    
<book ISBN="11-000000-003" 
        title
="Cooking on a Budget" 
        price
="23.95" >
   
<comments>
      
<userComment rating="4" 
          comment
="Delicious!" />
    
</comments>
    
</book>
    
<book ISBN="11-000000-004" 
        title
="Great Works of Art" 
        price
="29.95" >
   
</book>
</bookstore>
    这是MSDN里介绍相应控件的一个示例文件。
    从文件可以看出,book节点是一个实体(不知道叫什么名好)。其下有几个属性:ISBN,title,price等等。这种类型文件的特点是:所有的属性的值都在节点里包括,如<book ISBN="11-000000-004"   title="Great Works of Art" price="29.95" ></book>,即属性值是写在<book的后面,>符号之前。可能这也是标准的XML文件的格式,具体就不了解了。因为另一种格式在asp.net里不认,需要转换,所以我猜这种是标准格式。
    好了,再看看另外一种格式:
示例2:
<?xml version="1.0" standalone="yes"?>
<bookstore>
    
<book ISBN="10-000000-001">
        
<title>The Iliad and The Odyssey</title>
        
<price>12.95</price>
        
<comments>
            
<userComment rating="4">
                Best translation I've read.
            
</userComment>
            
<userComment rating="2">
                I like other versions better.
            
</userComment>
        
</comments>
    
</book>
    
<book ISBN="10-000000-999">
        
<title>Anthology of World Literature</title>
        
<price>24.95</price>
        
<comments>
            
<userComment rating="3">
                Needs more modern literature.
            
</userComment>
            
<userComment rating="4">
                Excellent overview of world literature.
            
</userComment>
        
</comments>
    
</book>
    
<book ISBN="11-000000-002">
        
<title>Computer Dictionary</title>
        
<price>24.95</price>
        
<comments>
            
<userComment rating="3">
               A valuable resource.
            
</userComment>
        
</comments>
    
</book>
    
<book ISBN="11-000000-003">
        
<title>Cooking on a Budget</title>
        
<price>23.95</price>
        
<comments>
            
<userComment rating="4">Delicious!</userComment>
        
</comments>
    
</book>
    
<book ISBN="11-000000-004">
        
<title>Great Works of Art</title>
        
<price>29.95</price>
    
</book>
</bookstore>
    通过对比上面两段代码就可以看出其差别了。示例2中的文件感觉更标记化(自己定的名字,呵呵),即,所以的属性都是以<>这样的标记命名,其值是包含在<></>这两个标记之间。如<title>Great Works of Art</title>,我个人感觉这种方式比较好理解。节点以<book>开始,期间的内容为它的属性。但是直接用这种格式的XML文件的话,用xmldatasource控件读不出来的(故在上面我称这种为非标准的XML格式,呵呵)。
    针对上面示例1中的数据,直接用XmlDataSource控件即可读出数据,而示例2中的数据需要经过转换才能用于XmlDataSource控件。转换文件Bookstore2.xsl(注意扩展名)示例:
<?xml version="1.0"?>
<xsl:stylesheet 
   
version="1.0"
   xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
   xmlns:msxsl
="urn:schemas-microsoft-com:xslt"
>
   
<xsl:strip-space elements="*"/>
   
<xsl:output method="xml" 
       omit-xml-declaration
="yes" 
       indent
="yes" 
       standalone
="yes" />

   
<xsl:template match="/">
      
<xsl:for-each select="bookstore">
         
<xsl:element name="bookstore">
            
<xsl:for-each select="book">
               
<xsl:element name="book">
                  
<xsl:attribute name="ISBN">
                     
<xsl:value-of select="@ISBN"/>
                  
</xsl:attribute>
                  
<xsl:attribute name="title">
                     
<xsl:value-of select="title"/>
                  
</xsl:attribute>
                  
<xsl:attribute name="price">
                     
<xsl:value-of select="price"/>
                  
</xsl:attribute>
               
</xsl:element>
            
</xsl:for-each>
         
</xsl:element>
      
</xsl:for-each>
   
</xsl:template>
</xsl:stylesheet>
    注意:在MSDN里这个文件是错误的。它的结尾标记是</xsl:transform>而不是</xsl:stylesheet>,在示例中我已改正。
    解读这个文件,可以看出:
    1.第一部分不去说了,应该是一些标准之类的,我直接拷贝过来了。
    2.第二部分
        <
xsl:strip-space elements="*"/>
   
<xsl:output method="xml" 
       omit-xml-declaration
="yes" 
       indent
="yes" 
       standalone
="yes" />
      
这一部分指的应该是转换后的输出格式的设置,也直接拷贝过来用,不去管它。
    3.从<xsl:template match="/">后边即为具体文件的定义了。这个match我猜应该是指的目录结构的位置吧,用“/"指顶层根节点。后边的就是定义元素了,元素的属性分两种,一种是如示例1中的,把属性直接包括在了<>标记里面,这种需要在设置value的时候用<xsl:value-of select="@ISBN"/>这种方式,另一种就是标记之间的,直接写属性的名字就行了。
    通过上述设置后,就可以在xmldatasource里用这种类型的XML文件了。设置它的datafile为你的XML文件,设置它的transformfile为你的这个xls文件。

参考文献:
MSDN
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_vwdcon/html/9f340b14-6e25-4e5a-b006-ef0bf395b1f4.htm
原文地址:https://www.cnblogs.com/erqie/p/1144167.html