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>

最新文章

  1. html5shiv.js and respond.min.js
  2. angular手势事件之on-Hold
  3. hdu 1203 概率+01背包
  4. SSL使用windows证书库中证书实现双向认证
  5. How can I add a new user as sudoer using the command line?
  6. Eclipse 安装反编译插件jadclipse(经验总结)
  7. sqlserver 存储过程中拼接sql语句 动态执行
  8. 一个jquery的图片下拉列表 ddSlick
  9. Fedora 18 安装前指南
  10. Android自定义View——自定义搜索框(SearchView)
  11. 如何在MyEclipse中部署struts2的环境
  12. 在IAR下移植CC2650 contiki工程
  13. 腾讯工蜂Git关联Jenkins Hooks
  14. live 2d js demo
  15. 开发Spring过程中几个常见异常(三):java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.edu.aop.ArithmeticCalculatorImpl at com.edu.aop.Main.main(Main.java:11)
  16. 通过django的rest-framework……(CBV)
  17. Python 远程桌面协议RDPY简介
  18. java JDBC (四)
  19. js的基本包装类型
  20. 【洛谷p1403 】【AHOI2005】约数研究

热门文章

  1. LA 4975
  2. 【Java】java数据库连接池配置的几种方法
  3. html5自定义标签选择器
  4. Android开源项目发现--- 工具类Log篇(持续更新)
  5. wpf 依赖性属性
  6. eclipse报错 com/genuitec/eclipse/j2eedt/core/J2EEProjectUtil 转
  7. LoadRunner的场景设置
  8. Android protectionLevel
  9. leetcode distinct-subsequences(DP)
  10. JavaScript高级程序设计18.pdf