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的更多相关文章

  1. 第一百二十六节,JavaScript,XPath操作xml节点

    第一百二十六节,JavaScript,XPath操作xml节点 学习要点: 1.IE中的XPath 2.W3C中的XPath 3.XPath跨浏览器兼容 XPath是一种节点查找手段,对比之前使用标准 ...

  2. XPath操作XML文档

    NET框架下的Sytem.Xml.XPath命名空间提供了一系列的类,允许应用XPath数据模式查询和展示XML文档数据. 3.1XPath介绍 主要的目的是在xml1.0和1.1文档节点树种定位节点 ...

  3. java使用dom4j和XPath解析XML与.net 操作XML小结

    最近研究java的dom4j包,使用 dom4j包来操作了xml 文件 包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), ...

  4. C#基础知识---Linq操作XML文件

    概述 Linq也就是Language Integrated Query的缩写,即语言集成查询,是微软在.Net 3.5中提出的一项新技术. Linq主要包含4个组件---Linq to Objects ...

  5. C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍

    一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...

  6. 黄聪:C#操作xml SelectNodes,SelectSingleNode通过 xPath 定位class包含Contains的DIV

    一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...

  7. C#操作Xml:XPath语法 在C#中使用XPath示例

    XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识. 示例Xml: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. php : DOM 操作 XML

    DOM 操作 XML 基本用法 XML文件: person.XML <?xml version="1.0" encoding="utf-8" ?> ...

  9. php操作xml

    最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星期的基本语法后做些小练习热热身,但是期间是各种问题啊,主要是对php不熟悉, ...

随机推荐

  1. 【转】不同VLAN之间相互通信及VTP、STP、EtherChannel概念

    厘清最后一个概念. 转了网上两个相关帖子: http://www.net130.com/CMS/Pub/Tech/tech_zh/2009_03_12_97386_3.htm http://blog. ...

  2. JavaWeb 文件上传 commons_fileupload方式

    import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadExcept ...

  3. iframe详细用法

    <iframe>是框架的一种形式,也比较常用到. 例子1.<iframe width=420 height=330 frameborder=0 scrolling=auto src= ...

  4. swiper去除滑动设置

    有时候使用swiper并不想让它滑动,怎么设置呢? 1.noSwiping设为true 2.在slide上(或其他元素)增加类名'swiper-no-swiping',使该slide无法拖动. 案例: ...

  5. 一起啃PRML - Preface 前言

    一起啃PRML - 前言 Preface @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ PRML,Pattern Recognition and M ...

  6. HDU-3854 LOOPS

    http://acm.hdu.edu.cn/showproblem.php?pid=3853 LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memo ...

  7. Android学习笔记(七)两个Fragment简单跳转示例

    在前两篇博文中分别介绍了Fragment得基础和Fragment的生命周期,然而说了这么多Fragment到底怎么用呢以及我们为什么要使用Fragment?本篇博文将主要探讨这两个问题,首先说下在AP ...

  8. [原]ubuntu下制作openstack-havana源

    ubuntu下可以用apt-mirror下载openstack的源: 1.安装apt-mirror: apt-get install apt-mirror 2.配置/etc/apt/mirror.li ...

  9. ubuntu 100M 到 10M

    浅析ubuntu下如何修改网卡网速--将100M网卡改为10M网卡 公司的路由器可能比较陈旧,机器启动之后,默认网卡是100M的,但是登录QQ经常出现掉线现象,后来得知原来需要将100M网卡降频到10 ...

  10. [转载]opencv MSER

    最大稳定极值区域(MSER-Maximally Stable Extremal Regions)可以用于图像的斑点区域检测.该算法最早是由Matas等人于2002年提出,它是基于分水岭的概念. MSE ...