xpath 随笔

    由XmlDocument 对象创建的XPathNavigator 对象是可编辑的, 由XPathDocument 对象创建的XPathNavigator 对象是只读的。XPathNavigator 的 CanEdit 属性让您判断。 

    XPathDocument 对 XPath 和 XSLT 中的使用进行了优化,而且是只读模式,并且还可以在对xml文档运行XPath或者对内存中的xml运行XSLT的时候提供更好的性能。这些情况下,XPathDocument 应该在 XmlDocument之前优先考虑,只有在转换之前需要更新xml文档的时候才考虑使用XmlDocument.

xml格式的数据源有在前台有两种绑定形式,一种是<# XPath("key")> ,另一种是<%# XPathSelect("key") %>
下面举例说明:
<% @ Page Language = " C# "   %>
<% @ Import Namespace = " System.Xml "   %>
< script runat = " server " >
  
private  XmlDataDocument xd  =   new  XmlDataDocument();
  
protected   void  Page_Load( object  sender, EventArgs e)
  
{
    xd.Load(Server.MapPath(
"XmlData.xml"));
    Repeater1.DataSource 
= xd.SelectNodes("//site");
    Repeater1.DataBind();
  }

</ script >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
  
< title > XPath 方法绑定 </ title >
</ head >
< body >
  
< form id = " form1 "  runat = " server " >
    
< asp:Repeater ID = " Repeater1 "  runat = " server " >
      
< ItemTemplate >
        
< a href = " <%#XPath( " url " ) %> "  title = " <%#XPath( " name " )%> " >
          
<% #XPath( " img " ==   null   ?  XPath( " name " ) :  " <img src=' "   +  XPath( " img " +   " ' style='border:0px'> " %>
        
</ a >
        
< br  />
        
< br  />
      
</ ItemTemplate >
    
</ asp:Repeater >
  
</ form >
</ body >
</ html >

<? xml version="1.0" encoding="utf-8"  ?>
< sites >
  
< site >
    
< name > .NET 开发者园地 </ name >
    
< url > http://dotnet.aspx.cc/ </ url >
    
< img > ../images/logo.gif </ img >
  
</ site >
  
< site >
    
< name > .NET 技术交流社区 </ name >
    
< url > http://community.csdn.net/ </ url >
    
< img > ../images/logo_csdn.gif </ img >
  
</ site >
  
< site >
    
< name > 播客站点 </ name >
    
< url > http://blog.csdn.ent/net_lover/ </ url >
  
</ site >
</ sites >

<% @ Page Language = " C# " %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > TemplateControl XPath and XPathSelect Example </ title >
</ head >
< body >
    
< h3 > TemplateControl XPath Example </ h3 >
    
< form id = " form1 "  runat = " server " >
    
< div >
      
< asp:XmlDataSource
        id
= " XmlDataSource1 "  
        runat
= " server "
        XPath
= " contacts "  
        DataFile
= " contacts.xml "   />     
      
< asp:FormView 
        id
= " FormView1 "  
        runat
= " server "  
        DataSourceID
= " XmlDataSource1 " >
        
< ItemTemplate >
          
< hr  />
          
< asp:Repeater 
            id
= " Repeater1 "  
            runat
= " server "  
            DataSource
= ' <%# XPathSelect("contact") %> '   >
            
< ItemTemplate >
              Name: 
<% # XPath( " name " %>   < br  />
              Note: 
<% # XPath( " note " %>   < br  />
              
< hr  />
            
</ ItemTemplate >
          
</ asp:Repeater >
        
</ ItemTemplate >
      
</ asp:FormView >
    
</ div >
    
</ form >
</ body >
</ html >


< contacts >
   
< contact  id ="1" >
     
< name > contact name 1 </ name >
     
< note > contact note 1 </ note >
   
</ contact >
   
< contact  id ="2" >
     
< name > contact name 2 </ name >
     
< note > contact note 2 </ note >
   
</ contact >
</ contacts >

你可能感兴趣的:(xpath)