C#读取与修改XML文档

在项目开发中,对XML文档的操作是很常用的,这里,简单的说明读取与修改XML文档

XML文档的格式

<?xml version="1.0" encoding="utf-8"?>
<webinfo>
  <web>
    <webname>谷歌</webname>
    <weburl>http://www.chinaz.com/web/2013/1211/3307662.shtml</weburl>
    <sendusername>谷歌小明</sendusername>
    <sendusermail>553325869@qq.com</sendusermail>
    <mailtitle>谷歌无法访问</mailtitle>
    <mailcontent>谷歌无法访问</mailcontent>
    <httpcode>404</httpcode>
    <httpresult>NotFound</httpresult>
    <httpdescript>未找到,服务器找不到请求的网页。</httpdescript>
    <webstate>失败</webstate>
    <ismail>已发送</ismail>
  </web>
</webinfo>

定义一个实体类,实现数据的传递

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

namespace WebDetection.Model
{
    public class WebXMLmodel
    {
        public WebXMLmodel() { }
        public string webname { get; set; }
        public string weburl { get; set; }
        public string sendusername { get; set; }
        public string sendusermail { get; set; }
        public string mailtitle { get; set; }
        public string mailcontent { get; set; }
        public string httpcode { get; set; }
        public string httpresult { get; set; }
        public string httpdescription { get; set; }
        public string Webstate { get; set; }
        public string ismail { get; set; }
    }
}

读取XML文档

#region 读取xml
        /// <summary>
        /// 读取xml
        /// </summary>
        /// <returns></returns>
        public static List<WebXMLmodel> readXML()
        {
            // 采用XmlDocument操作XML
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\Users\楠\Desktop\web\WebDetection\WebDetection\WebInfo\WebXML.xml");
            // 实例化实体类
            WebXMLmodel webxmlmode = new WebXMLmodel();
            // 返回泛型
            List<WebXMLmodel> webxmllist = new List<WebXMLmodel>();

            // 获取根节点
            XmlNode root = doc.SelectSingleNode("webinfo");
            // 获取根节点下的所有子节点
            XmlNodeList child = root.ChildNodes;

            // 循环遍历读取XML文件
            foreach (XmlNode children in child)
            {
                // 得到web节点的所有子节点
                XmlNodeList childrens = children.ChildNodes;
                // 获取每一个子节点的值
                webxmlmode.webname = childrens.Item(0).InnerText;
                webxmlmode.weburl = childrens.Item(1).InnerText;
                webxmlmode.sendusername = childrens.Item(2).InnerText;
                webxmlmode.sendusermail = childrens.Item(3).InnerText;
                webxmlmode.mailtitle = childrens.Item(4).InnerText;
                webxmlmode.mailcontent = childrens.Item(5).InnerText;
                webxmlmode.httpcode = childrens.Item(6).InnerText;
                webxmlmode.httpresult = childrens.Item(7).InnerText;
                webxmlmode.httpdescription = childrens.Item(8).InnerText;
                webxmlmode.Webstate = childrens.Item(9).InnerText;
                webxmlmode.ismail = childrens.Item(10).InnerText;
                webxmllist.Add(webxmlmode);
            }
            return webxmllist;
        }
        #endregion

修改XML文档

#region 修改xml
        /// <summary>
        /// 修改xml
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool writeXML(List<WebXMLmodel> list)
        {
            // 采用XmlDocument操作XML
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\Users\楠\Desktop\web\WebDetection\WebDetection\WebInfo\WebXML.xml");

            // 获取根节点
            XmlNode root = doc.SelectSingleNode("webinfo");
            // 获取根节点下的所有子节点
            XmlNodeList child = root.ChildNodes;
            int count=list.Count();
            count=0;

            // 循环遍历修改XML文件
            foreach (XmlNode children in child)
            {
                // 得到web节点的所有子节点
                XmlNodeList childrens = children.ChildNodes;
                // 获取每一个子节点的值
                childrens.Item(6).InnerText=list[count].httpcode;
                childrens.Item(7).InnerText = list[count].httpresult;
                childrens.Item(8).InnerText = list[count].httpdescription;
                childrens.Item(9).InnerText = list[count].Webstate;
                childrens.Item(10).InnerText = list[count].ismail;
                count=count+1;
            }

            // 保存修改
            doc.Save(@"C:\Users\楠\Desktop\web\WebDetection\WebDetection\WebInfo\WebXML.xml");
            return true;
        }
        #endregion

实现对XML文档操作的方法有很多种,上面仅仅是其中一种实现方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值