曹永思

导航

ASP.NET 操作XML

//调用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>

posted on 2012-07-30 15:04  曹永思  阅读(345)  评论(0编辑  收藏  举报