• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#递归读写XML文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

由于所需要操作的XML文件结点格式不统一,故写了一个通用递归类,代码如下:

一、XML文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <Cycles>
      <SY_DT5_GSM_Month>5</SY_DT5_GSM_Month>
      <SY_DT5_GSM_Day>50</SY_DT5_GSM_Day>
    </Cycles>
  </appSettings>
  <Transmissions>
    <GSM>
      <SY_DT5_GSM Company="" Model="" COM="COM3">
        <YesterDay>2011-07-16</YesterDay>
        <LastMonth>
        </LastMonth>
      </SY_DT5_GSM>
    </GSM>
    <GRPS>
    </GRPS>
    <IP>
    </IP>
  </Transmissions>
  <connectionStrings>
    <Server>.</Server>
    <DataBase>Voltage</DataBase>
    <User>sa</User>
    <Pass>
    </Pass>
  </connectionStrings>
</configuration>

二、递归类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
///XmlRWrite 的摘要说明
/// </summary>
public class XmlRWrite
{
    private string _FilePath;
    private string _ParentNode;
    private string _ChildName;
    private string _AttributeName;

    public XmlRWrite()
    {

    }

    /// <summary>
    /// XML文件存储路径
    /// </summary>
    public string FilePath
    {
        set
        {
            _FilePath = value;
        }
    }

    /// <summary>
    /// 父结点
    /// </summary>
    public string ParentNode
    {
        set
        {
            _ParentNode = value;
        }
    }

    /// <summary>
    /// 子结点
    /// </summary>
    public string ChildName
    {
        set
        {
            _ChildName = value;
        }
    }

    /// <summary>
    /// 结点属性
    /// </summary>
    public string AttributeName
    {
        set
        {
            _AttributeName = value;
        }
    }

    /// <summary>
    /// 递归读取XML文件结点值
    /// </summary>
    /// <param name="XNL">XmlNodeList列表</param>
    /// <returns>返回结点值</returns>
    private string ReadNodeValue(XmlNodeList XNL)
    {
        string Value = "";
        for (int i = 0; i < XNL.Count; i++)
        {
            if (Value != "")
                break;
            if (XNL[i].Name == _ParentNode)
            {
                foreach (XmlNode Childnode in XNL[i])
                {
                    if (Childnode.Name == _ChildName)
                    {
                        Value = Childnode.InnerText;
                        break;
                    }
                }
            }
            else
            {
                XmlNodeList xnl = XNL[i].ChildNodes;
                if (xnl.Count > 0)
                    Value = ReadNodeValue(xnl);
            }
        }
        return Value;
    }

    /// <summary>
    /// 递归读取XML文件结点属性值
    /// </summary>
    /// <param name="XNL">XmlNodeList列表</param>
    /// <returns>返回结点值</returns>
    private string ReadAttributeValue(XmlNodeList XNL)
    {
        string Value = "";
        for (int i = 0; i < XNL.Count; i++)
        {
            if (Value != "")
                break;
            if (XNL[i].Name == _ParentNode)
            {
                foreach (XmlNode Childnode in XNL[i])
                {
                    if (Childnode.Name == _ChildName)
                    {
                        Value = Childnode.Attributes[_AttributeName].Value;
                        break;
                    }
                }
            }
            else
            {
                XmlNodeList xnl = XNL[i].ChildNodes;
                if (xnl.Count > 0)
                    Value = ReadAttributeValue(xnl);
            }
        }
        return Value;
    }

    /// <summary>
    /// 递归设置结点值
    /// </summary>
    /// <param name="Value">结点值</param>
    /// <param name="XNL">XmlNodeList列表</param>
    private void WriteNodeValue(string Value, XmlNodeList XNL)
    {
        bool bl = false;
        for (int i = 0; i < XNL.Count; i++)
        {
            if (bl)
                break;
            if (XNL[i].Name == _ParentNode)
            {
                foreach (XmlNode Childnode in XNL[i])
                {
                    if (Childnode.Name == _ChildName)
                    {
                        Childnode.InnerText = Value;
                        bl = true;
                        break;
                    }
                }
            }
            else
            {
                XmlNodeList xnl = XNL[i].ChildNodes;
                if (xnl.Count > 0)
                    WriteNodeValue(Value, xnl);
            }
        }
    }

    /// <summary>
    /// 递归设置结点属性值
    /// </summary>
    /// <param name="Value">属性值</param>
    /// <param name="XNL">XmlNodeList列表</param>
    private void WriteAttributeValue(string Value, XmlNodeList XNL)
    {
        bool bl = false;
        for (int i = 0; i < XNL.Count; i++)
        {
            if (bl)
                break;
            if (XNL[i].Name == _ParentNode)
            {
                foreach (XmlNode Childnode in XNL[i])
                {
                    if (Childnode.Name == _ChildName)
                    {
                        Childnode.Attributes[_AttributeName].Value = Value;
                        bl = true;
                        break;
                    }
                }
            }
            else
            {
                XmlNodeList xnl = XNL[i].ChildNodes;
                if (xnl.Count > 0)
                    WriteAttributeValue(Value, xnl);
            }
        }
    }

    private XmlNodeList XNodeList()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(_FilePath);
        XmlNodeList XNL = xmlDoc.SelectSingleNode("configuration").ChildNodes;
        return XNL;
    }

    /// <summary>
    /// 递归读取结点值
    /// </summary>
    /// <returns>返回值</returns>
    public string GetNodeValue()
    {
        XmlNodeList XNL = XNodeList();
        string Value = ReadNodeValue(XNL);       
        return Value;
    }

    /// <summary>
    /// 递归读取属性值
    /// </summary>
    /// <returns>返回值</returns>
    public string GetAttributeValue()
    {
        XmlNodeList XNL = XNodeList();
        string Value = ReadAttributeValue(XNL);
        return Value;
    }

    /// <summary>
    /// 递归设置结点值
    /// </summary>
    /// <param name="Value">结点值</param>
    public void SetNodeValue(string Value)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(_FilePath);
        XmlNodeList XNL = xmlDoc.SelectSingleNode("configuration").ChildNodes;
        WriteNodeValue(Value, XNL);
        xmlDoc.Save(_FilePath);
    }

    /// <summary>
    /// 递归设置结点属性值
    /// </summary>
    /// <param name="Value">属性值</param>
    public void SetAttributeValue(string Value)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(_FilePath);
        XmlNodeList XNL = xmlDoc.SelectSingleNode("configuration").ChildNodes;
        WriteAttributeValue(Value, XNL);
        xmlDoc.Save(_FilePath);
    }
}

三、使用方法

 protected void Button1_Click(object sender, EventArgs e)
    {
        string path = "C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\WebSites\\WebSite2\\setup.xml";

        XmlRWrite xmlRWrite = new XmlRWrite();
        xmlRWrite.FilePath = path;

        //读取结点值
        xmlRWrite.ParentNode = "SY_DT5_GSM";
        xmlRWrite.ChildName = "YesterDay";
        LblMsg.Text = xmlRWrite.GetNodeValue();
        //读取属性值
        xmlRWrite.ParentNode = "GSM";
        xmlRWrite.ChildName = "SY_DT5_GSM";
        xmlRWrite.AttributeName = "COM";
        LblMsg.Text = xmlRWrite.GetAttributeValue();

        //设置结点值
        xmlRWrite.ParentNode = "SY_DT5_GSM";
        xmlRWrite.ChildName = "YesterDay";
        xmlRWrite.SetNodeValue("2011-07-16");
        //设置属性值
        xmlRWrite.ParentNode = "GSM";
        xmlRWrite.ChildName = "SY_DT5_GSM";
        xmlRWrite.AttributeName = "COM";
        xmlRWrite.SetAttributeValue("COM3");

    }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用.net(C#)发送邮件学习手册(带成功案例发布时间:2022-07-13
下一篇:
c#float显示时保存一位小数发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap