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

ASP.NET操作XML

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

//调用XML的方法

 XMLReader rd = new XMLReader("/xml/user.xml");
    return rd.SelectNode("节点[@属性名=\"" + 属性值 + "\"]", "节点中的子节点名");

///操作XML的类

using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Xml;
using log4net;

namespace Apps_Factory.Common
{

    public class XMLReader
    {
        public static readonly ILog exlog = null;
        private XmlDocument xmlDoc;
        private string xmlpath;
        static XMLReader()
        {
            exlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }
        public XMLReader(string path)
        {
            try
            {
                xmlpath = HttpContext.Current.Server.MapPath(path);
                xmlDoc = new XmlDocument();
                if (xmlpath != null)
                {
                    xmlDoc.Load(xmlpath);
                }
            }
            catch (Exception ex) { }
        }
        public XMLReader(string path, int type)
        {
            try
            {
                xmlDoc = new XmlDocument();
                if (path != null)
                {
                    xmlDoc.Load(path);
                }
            }
            catch (Exception ex) { }
        }
        /// <summary>
        /// 获取节点集
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public XmlNodeList GetXMLFileodes(string xpath)
        {
            if (xmlDoc != null)
            {
                return xmlDoc.SelectNodes(xpath);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取指定节点的指定项名
        /// </summary>
        /// <param name="nodeName"></param>
        /// <param name="attribute"></param>
        /// <param name="attributeValue"></param>
        /// <param name="dataAttribute"></param>
        /// <returns></returns>
        public string GetDataFromxml(string nodeName, string attribute, string attributeValue, string dataAttribute)
        {
            if (xmlDoc != null)
            {
                XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeName).ChildNodes;//获取bookstore节点的所有子节点   
                foreach (XmlNode xn in nodeList)//遍历所有子节点   
                {
                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型   
                    if (xe.GetAttribute(attribute) == attributeValue)//如果genre属性值为“李赞红”   
                    {
                        //xe.SetAttribute("genre", "update李赞红");//则修改该属性为“update李赞红”   
                        XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点   
                        foreach (XmlNode xn1 in nls)//遍历   
                        {
                            XmlElement xe2 = (XmlElement)xn1;//转换类型   
                            if (xe2.Name == dataAttribute)//如果找到   
                            {
                                return xe2.InnerText;
                            }
                        }
                        break;
                    }
                }
                return "";
                // xmlDoc.Save(path);//保存。
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 选择node
        ///  "/节点   [@属性   =   属性值   ]"   
        /// 根节点[子节点/子节点='值']  
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        public XmlNode SelectNode(string select)
        {
            XmlNode selectNode;
            if (xmlDoc != null)
            {
                selectNode = xmlDoc.SelectSingleNode("descendant::" + select);
                if (selectNode == null)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
            return selectNode;
        }
        /// <summary>
        /// 选择节点
        /// </summary>
        /// <param name="select">根节点[子节点/子节点='值']</param>
        /// <param name="nodeName">具体某个节点名称</param>
        /// <returns>返回node节点的值</returns>
        public string SelectNode(string select, string nodeName)
        {
            XmlNode selectNode;
            string nodeInntext;
            if (xmlDoc != null)
            {
                try
                {
                    selectNode = xmlDoc.SelectSingleNode("descendant::" + select);
                    if (selectNode == null)
                    {
                        return "";
                    }
                    else
                    {
                        try
                        {
                            nodeInntext = selectNode.SelectSingleNode("descendant::" + nodeName).InnerText;
                        }
                        catch (Exception e)
                        {
                            exlog.Error(e.Message);
                            return "";
                        }

                    }
                }
                catch (Exception ex)
                {
                    exlog.Error(ex.Message);
                    return "";
                }
            }
            else
            {
                return "";
            }
            return nodeInntext;
        }
        /// <summary>
        /// innertext
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public string GetXMLNodeText(XmlNode n)
        {
            if (n != null)
            {
                return n.InnerText;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// attribute value
        /// </summary>
        /// <param name="n"></param>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public string GetXMLNodeText(XmlNode n, string attribute)
        {
            if (n != null)
            {
                return n.Attributes[attribute].Value;
            }
            else
            {
                return "";
            }
        }
    }
}

//XML文件

<?xml version="1.0" encoding="utf-8"?>
<user>
  <haoma v="1">
    <title>11111</title>
    <values>1您本月命犯桃花哦</values>
  </haoma>
  <haoma v="2">
    <title>22222</title>
    <values>2您本月命犯桃花哦</values>
  </haoma>
  <haoma v="3">
    <title>33333</title>
    <values>3您本月命犯桃花哦</values>
  </haoma>
  <haoma v="4">
    <title>4444</title>
    <values>4您本月命犯桃花哦</values>
  </haoma>
  <haoma v="5">
    <title>55555</title>
    <values>5您本月命犯桃花哦</values>
  </haoma>
  <haoma v="6">
    <title>66666</title>
    <values>6您本月命犯桃花哦</values>
  </haoma>
  <haoma v="7">
    <title>77777</title>
    <values>7您本月命犯桃花哦</values>
  </haoma>
  <haoma v="8">
    <title>88888</title>
    <values>8您本月命犯桃花哦</values>
  </haoma>
  <haoma v="9">
    <title>999999</title>
    <values>9您本月命犯桃花哦</values>
  </haoma>
</user>


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
自学MVC看这里——全网最全ASP.NETMVC教程汇总发布时间:2022-07-10
下一篇:
ASP.NET Web API 简介发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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