zoukankan      html  css  js  c++  java
  • xpath 操作XML

    1、xpath 操作XML,底下部分代码被注释了,但是是完整功能,去除注释是正常使用的(有写命名和其他冲突,所以注释了)

    总体有:完整读取xml,对xml的增删改查,对xml的特定操作

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

    namespace XPath读取XML.Controllers
    {
    public class XpathReadXMLController : Controller
    {


    //
    // GET: /XpathReadXML/

    public ActionResult Index()
    {

    #region 完整化原生态读取XML


    //梳理常规xml的用法,属性,构造函数,再使用xpath查询
    //string path = Server.MapPath("Xml/3502100001.config");
    //XDocument xdoc = XDocument.Load(path);

    //string info = xdoc.Declaration.ToString();//想要具体访问声明内部信息,可以查看xdoc.Declaration.Version,xdoc.Declaration.Encoding
    //string[] DeclarationInfo=new string[]{xdoc.Declaration.Version,xdoc.Declaration.Encoding};

    #region 学习方法,使用的
    //不懂的看定义,还不懂就是百度和MSDN的具体使用
    #endregion
    ///只要依次拿下对应的ELement,接着是Attribute什么,最后是value,什么都可以拿下来

    //XElement rootNode = xdoc.Root;

    //IEnumerable<XElement> eleList = rootNode.Elements("Serverip");

    //XElement xele = rootNode.Element("Serverip");

    //XAttribute rootNodeAttribute = rootNode.Element("DBAdmin").Attribute("type");//查找xml 中DBAdmin节点的属性这里是xml

    //string rootNodeAttributeValue = rootNode.Element("DBAdmin").Attribute("type").Value;//具体值

    //IEnumerable<XAttribute> rootNodeAttributeList = rootNode.Element("DBAdmin").Attributes("type");//这是一个结果集合


    #endregion

    #region 完整遍历XML(一层或者两层),存放至Dictionary
    //string path = Server.MapPath("Xml/3502100002.config");
    //XDocument xdoc = XDocument.Load(path);

    //Dictionary<string, string> dicXml = new Dictionary<string, string>();

    //XElement rootNode = xdoc.Root;

    //XNode xnode = rootNode.FirstNode;//如果内部有存在这个,那就是可以强制转换,获取内部的值 System.Xml.Linq.XElement


    //XElement xele = (XElement)xnode;
    //dicXml.Add(xele.Name.LocalName,xele.Value);
    //IEnumerable<XNode> xNodeList = rootNode.Nodes();


    //foreach (XNode x in xNodeList)
    //{
    // XElement xele = x as XElement;//转换后可以获取对应的Element的标签名称

    // if (xele != null)
    // {//过滤注释和空的
    // dicXml.Add(xele.Name.LocalName, xele.Value);
    // }

    //}

    #endregion

    return View();
    }

    public ActionResult Delete() {

    # region 增删改
    string path = Server.MapPath("Xml/350210000t.config");
    XDocument xdoc = XDocument.Load(path);

    Dictionary<string, string> dicXml = new Dictionary<string, string>();

    XElement rootNode = xdoc.Root;

    XNode xnode = rootNode.FirstNode;//如果内部有存在这个,那就是可以强制转换,获取内部的值 System.Xml.Linq.XElement

    //rootNode.SetElementValue("Node1", "测试新增节点");

    //rootNode.Save(path);
    //选中总结点,再加入
    xdoc.Element("GWT").SetElementValue("Node1", "加入新的节点");//这个新增|修改|删除都可以
    xdoc.Element("GWT").SetElementValue("Node1", null);//删除,Ps:其他增删改,还其他方法,remove,add,Repalce

    xdoc.Element("GWT").SetElementValue("NOde1", "修改");//修改

    xdoc.Save(path);//这句很重要,这是最后对本地文件做修改
    #region 部分修改

    //XElement xele = xdoc.Element("GWT").Element("Node1");
    //xele.SetElementValue("Node11", "测试新增节点");

    //xele.Save(path);//注意这个save只会把部分的修改的东西保存文件中,所以要使用原来全局xml
    #endregion
    #endregion
    return View();
    }


    public ActionResult XpathIndex() {

    #region Xpath的使用
    string path = Server.MapPath("Xml/350210000t.config");


    XmlDocument xmlDoc = new XmlDocument();//XM Lyuanshengtai

    xmlDoc.Load(path);

    XmlNodeList xmlList = xmlDoc.SelectNodes("/GWT");

    XmlNode node = xmlList[0];

    XmlNodeList xmlNodeList = xmlDoc.SelectNodes("./GWT");//当前上下文中的所有 <GWT> 元素。
    XmlNodeList xmlNodeAttrButeList = xmlDoc.SelectNodes("//Serverip[@type='DataBaseInfo'and @Description='LinkUrl']");//选择时候多属性,不能使用&& 这类,而是直接使用and,or

    #endregion

    return View();


    }
    }
    }

    2、例子中使用的xml

    <?xml version="1.0" encoding="utf-8"?>
    <GWT>
    <declarecode>3502100001</declarecode>
    <ownercode>3502100001</ownercode>
    <TradeCode>3502100001</TradeCode>


    <!--111-->
    <DBAdmin type="UserInfo" Description="UserName">sa</DBAdmin>
    <DBPwd>gwt123456</DBPwd>
    <ClientNum />
    <Admin>admin</Admin>
    <Password>gwt123456</Password>
    <FinishDate>2015/12/30 0:00:00</FinishDate>
    <DBType />
    <Serverip type="DataBaseInfo" Description="LinkUrl">
    <link>192.168.1.111,1433</link>
    <Info>这是访问数据库的地址</Info>
    </Serverip>
    <ServerPort />
    <status>0</status>
    <TrustType />
    <EntVersion>EB1</EntVersion>
    <IsPay />
    <UserType>EB1</UserType>
    <IsMD5>1</IsMD5>

    </GWT>

  • 相关阅读:
    [转]JavaWeb学习总结(五十)——文件上传和下载
    [转]JavaWeb学习总结(四十九)——简单模拟Sping MVC
    [转]JavaWeb学习总结(四十八)——模拟Servlet3.0使用注解的方式配置Servlet
    [转]javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
    [转]javaweb学习总结(四十六)——Filter(过滤器)常见应用
    [转]javaweb学习总结(四十五)——监听器(Listener)学习二
    js数组的方法
    神策埋点
    微信分享
    微信小程序
  • 原文地址:https://www.cnblogs.com/linbin524/p/4767079.html
Copyright © 2011-2022 走看看