Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
当前位置: 主页 > vs2010教程 >

Visual Studio2010讲解C#4.0语法(2)--使用XQuery引擎操作XML文档

时间:2010-08-06 18:18来源:blog.csdn.net/yincheng01 作者:admin 点击:

目前,市面上的XML数据库,尤其是源生XML数据库(Native XML Database)如Ipedo XML Database、Software AG Tamino Server、Berkeley XML Database都提供了XQuery支持,用于查询存储在XML数据库中的XML片断或者XML节点。甚至Ipedo XML数据库还提供了XQuery Update功能,用于更新XML文档内容。下图是这类XQuery引擎的例示图。


 

这类XQuery引擎内嵌在XML数据库中,实现方面需要考虑内部存储的特性,如使用的数据结构、存储的XML Meta信息等。在查询优化方面需要考虑索引(Index)的使用,使用不使用索引以及使用哪一个索引。

下面我们来看下这方面的实例:

 首先打开Visual Studio2010创建一个基于C#的ConsoleApplication工程XQuery:

创建成功进入工程后,首先我们在工程项目下创建一个Data文件夹向其中添加一个bib.xml文件如下图所示:

 


然后打开bib.xml加入下列代码

<bib> 
  <book year="1994"> 
    <title>TCP/IP Illustrated</title> 
    <author> 
      <last>Stevens</last> 
      <first>W.</first> 
    </author> 
    <publisher>Addison-Wesley</publisher> 
    <price>65.95</price> 
  </book> 
 
  <book year="1992"> 
    <title>Advanced Programming in the Unix environment</title> 
    <author> 
      <last>Stevens</last> 
      <first>W.</first> 
    </author> 
    <publisher>Addison-Wesley</publisher> 
    <price>65.95</price> 
  </book> 
 
  <book year="2000"> 
    <title>Data on the Web</title> 
    <author> 
      <last>Abiteboul</last> 
      <first>Serge</first> 
    </author> 
    <author> 
      <last>Buneman</last> 
      <first>Peter</first> 
    </author> 
    <author> 
      <last>Suciu</last> 
      <first>Dan</first> 
    </author> 
    <publisher>Morgan Kaufmann Publishers</publisher> 
    <price>39.95</price> 
  </book> 
 
  <book year="1999"> 
    <title>The Economics of Technology and Content for Digital TV</title> 
    <editor> 
      <last>Gerbarg</last> 
      <first>Darcy</first> 
      <affiliation>CITI</affiliation> 
    </editor> 
    <publisher>Kluwer Academic Publishers</publisher> 
    <price>129.95</price> 
  </book> 
 
</bib> 

最后在Program.cs文件里写入如下代码:

using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Xml;  
using System.Linq;  
using System.Xml.Linq;  
 
namespace LinqToXmlSample  
{  
    class Program  
    {  
        static void Main(string [] args)   
        {  
            //列出所有Serge and Peter共同撰写的书籍  
            XDocument doc = XDocument.Load(SetDataPath() + "bib.xml");  
            var b1 = doc.Descendants("book")  
                        .Where(b => b.Elements("author")  
                                     .Elements("first")  
                                     .Any(f => (string)f == "Serge"));  
            var b2 = doc.Descendants("book")  
                        .Where(b => b.Elements("author")  
                                     .Elements("first")  
                                     .Any(f => (string)f == "Peter"));  
            var books = b1.Concat(b2);  
            foreach (var q in books)  
                Console.WriteLine(q);  
 
            Console.ReadLine();  
        }  
 
        static public string SetDataPath()  
        {  
            string path = Environment.CommandLine;  
            while (path.StartsWith("\""))  
            {  
                path = path.Substring(1, path.Length - 2);  
            }  
            while (path.EndsWith("\"") || path.EndsWith(" "))  
            {  
                path = path.Substring(0, path.Length - 2);  
            }  
            path = Path.GetDirectoryName(path);  
 
            return Path.Combine(path, "data\\");  
        }  
    }  


按下F5开始调试,运行界面如下:


 

 

(责任编辑:admin)
Tags:XML vs2010 C#4.0
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价