XPath 的简单应用(源代码)

本文介绍了一种从XML文件中读取数据并将其转换为DataTable的方法,同时提供了使用XPath进行节点选择的具体实现。该方法有助于高效处理XML数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Xml.XPath;
using System.IO;
namespace TestXPath
{
    
class XMLOpretion
    
{
        
///<summary>
        
/// recieve xml return DataTable, Static Method
        
///</summary>
        
/// <param name="url"> the path of xml document
        
/// </parm>

        public static DataTable FillDT(string url)
        
{
            StreamReader sr 
= new StreamReader(url);
            XmlDataDocument xd 
= new XmlDataDocument();
            xd.DataSet.ReadXml(sr);
            DataTable dt
=xd.DataSet.Tables[0];
            xd 
= null;
            sr.Close();
            
return dt;
        }

        
///<summary>
        
/// recieve xml,the XPath , return XPathNodeIterator, Static Method
        
///</summary>
        
/// <param name="url"> the path of xml document
        
/// </parm>
        
/// <param name="XPathSelect">the XPath select sentence</param>

        public static XPathNodeIterator XPathIter(string url, string XPathSelect)
        
{
            StreamReader sr 
= new StreamReader(url);
            XPathDocument doc 
= new XPathDocument(sr);
            XPathNavigator myNav 
= doc.CreateNavigator();
            XPathNodeIterator myIter 
= myNav.Select(XPathSelect);
            doc 
= null;
            sr.Close();
            
return myIter;
        }


    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值