• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小不点儿大的由我做主!
流年似水!
博客园    首页    新随笔    联系   管理    订阅  订阅

C#读取XML数据

最近刚学C#,很多地方不会,边学边写了一个类,用于读取xml文件里的数据。

xml文件定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
     <path id="1">c:\windows</path>
</configuration>

下面是读取xml文件的类:

Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace xml
{
    
class readXML
    {
        
private string filename; //文件路径
        public string str=null;

        
//构造函数,filename是xml文件的路径
        public readXML(string filename) {
            
this.filename = filename;
        }

        
public string getPath(string id, string str) {
            XmlTextReader reader 
= new XmlTextReader(filename);
           
            
while (reader.Read())
            {
                
if (reader.Name == str && reader.GetAttribute("id") == id)
                {
                    str 
= reader.ReadString();
                    reader.Close();
                    
return str;
                }
            }
            reader.Close();
            
return null;
        }
    }

 

此类的getPath()方法用于通过id值和path子元素,获得相应的元素值。

posted @ 2009-01-12 09:49  小不点儿大的由我做主!  阅读(451)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3