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>
xpath 操作XML的更多相关文章
- 第一百二十六节,JavaScript,XPath操作xml节点
第一百二十六节,JavaScript,XPath操作xml节点 学习要点: 1.IE中的XPath 2.W3C中的XPath 3.XPath跨浏览器兼容 XPath是一种节点查找手段,对比之前使用标准 ...
- XPath操作XML文档
NET框架下的Sytem.Xml.XPath命名空间提供了一系列的类,允许应用XPath数据模式查询和展示XML文档数据. 3.1XPath介绍 主要的目的是在xml1.0和1.1文档节点树种定位节点 ...
- java使用dom4j和XPath解析XML与.net 操作XML小结
最近研究java的dom4j包,使用 dom4j包来操作了xml 文件 包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), ...
- C#基础知识---Linq操作XML文件
概述 Linq也就是Language Integrated Query的缩写,即语言集成查询,是微软在.Net 3.5中提出的一项新技术. Linq主要包含4个组件---Linq to Objects ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- 黄聪:C#操作xml SelectNodes,SelectSingleNode通过 xPath 定位class包含Contains的DIV
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- C#操作Xml:XPath语法 在C#中使用XPath示例
XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识. 示例Xml: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- php : DOM 操作 XML
DOM 操作 XML 基本用法 XML文件: person.XML <?xml version="1.0" encoding="utf-8" ?> ...
- php操作xml
最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星期的基本语法后做些小练习热热身,但是期间是各种问题啊,主要是对php不熟悉, ...
随机推荐
- matlab的cell数组
matlab的cell数组 元胞数组: 元胞数组是MATLAB的一种特殊数据类型,可以将元胞数组看做一种无所不包的通用矩阵,或者叫做广义矩阵.组成元胞数组的元素可以是任何一种数据类型的常数或者常量,每 ...
- Technology Trader
zoj2071:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2071 题意:题意一些零件,每一个零件会有一个花费,然后用这 ...
- Palindrome
poj3974:http://poj.org/problem?id=3974 题意:求给定长度最长回文串的长度. 题解:直接套manacher,搞定. #include<iostream> ...
- 树莓派学习路程No.1 GPIO功能初识 wiringPi安装
WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi遵守GUN Lv3.wiringPi使用C或者C++开发并且可以被其他语言包转,例如python.ruby或者PHP等.Wiri ...
- Guava的一些总结
guava是java API蛋糕上的冰激凌(精华). 源码包的简单说明: com.google.common.annotations:普通注解类型. com.google.common.base: ...
- ANDROID 中UID与PID的作用与区别
PID:为Process Identifier, PID就是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID.进程中止后PID被系统回收,可能会被继续分配给新运行的程序,但是在a ...
- 【HDOJ】1043 Eight
这道题目最开始做的时候wa+TLE.后面知道需要状态压缩,最近A掉.并且练习一下各种搜索算法. 1. 逆向BFS+康拓展开. #include <iostream> #include &l ...
- Linux Kernel ‘perf’ Utility 本地提权漏洞
漏洞名称: Linux Kernel ‘perf’ Utility 本地提权漏洞 CNNVD编号: CNNVD-201309-050 发布时间: 2013-09-09 更新时间: 2013-09-09 ...
- Node.js权威指南 (3) - Node.js基础知识
3.1 Node.js中的控制台 / 19 3.1.1 console.log方法 / 19 3.1.2 console.error方法 / 20 3.1.3 console.dir方法 / 21 3 ...
- Perfect Squares——Leetcode
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...