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不熟悉, ...
随机推荐
- 保留你的dSYM文件
大家编译iPhone程序的时候,都会发现二进制文件的旁边生成了一个.dSYM文件.以前一直不知道这个文件是用来干嘛的,今天才知道这个是symbol file,用来debug用的. 大家可以读读这篇文档 ...
- ios开发UI篇—在ImageView中添加按钮以及Tag的参数说明
ios开发UI篇—在ImageView中添加按钮以及Tag的参数说明 一.tag参数 一个视图通常都只有一个父视图,多个子视图,在开发中可以通过使用子视图的tag来取出对应的子视图.方法为Viewwi ...
- Keil 代码折叠功能的使用
使用keil时将某段{......}内的代码折叠起来的方法:
- QT 读写二进制 (数值)高位在前
在人们的计数规则中,一般都认为高位在前,即往前的地位大,如123,我们认为是一百二十三, 但在计算机中数值是以二进制存储的,字节是最小的存储单位,如int(32位),占4个字节,每个字节有八位, 24 ...
- FILTER优化
explain plan for select a.* from fxqd_list_20131115_new_100 a where (acct_no, oper_no, seqno, trans_ ...
- configure: error: cannot find protoc, the Protocol Buffers compiler
centos 6 安装mosh 1.2 2012-05-07 17:21:41标签:centos mosh 关于mosh(引用于) 芬兰研究员Tatu Ylönen于1995年设计出最早的SSH协议, ...
- PHP 不安全文件权限漏洞
漏洞名称: PHP 不安全文件权限漏洞 CNNVD编号: CNNVD-201309-056 发布时间: 2013-09-09 更新时间: 2013-09-09 危害等级: 漏洞类型: 权限许可和 ...
- Bitmap 与Drawable相互转换
Drawable 转 Bitmap import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import ...
- 织梦CMS(dedecms)栏目属性及系统封面模板、列表模板、文章模板区别和路径设置解答
问题一:(织梦"栏目管理"的"常规选项"中3个栏目属性分析?) 织梦CMS的栏目属性分成三种, -->最终列表栏目 -->频道封面 -->外部 ...
- 把测试app打包成ipa文件
我终于把我的程序放到我的touch上了,其实把app放到touch上还有很多办法,这篇教程是主要讲怎么把app注册了,然后打包成一个ipa文件的. 先上官方文档:https://developer.a ...