C#读取XML节点内容方法实例简析
来源:互联网 发布:网络商店怎么开 编辑:程序博客网 时间:2024/05/29 16:01
http://xieycms.blog.163.com/blog/static/281213902009102035425825/
#读取XML节点内容方法实例入手让我们来看看C#读取XML节点的实现:
- using System;
- using System.Xml;
- using System.Xml.XPath;
- using System.Da
ta; - class ReadXML
- {
- public static void Main()
- {
- string sFile = "ReadXml.xml";
- //C#读取XML节点method 1
- XmlDocument doc = new XmlDocument();
- doc.Load(sFile);
- XmlNode node = doc.DocumentElement["News"]["Content"];
- Console.WriteLine(node.InnerText);
- //C#读取XML节点method2
- node = doc.SelectSingleNode("//Content");
- Console.WriteLine(node.InnerText);
- //similarly
- node = doc.DocumentElement.SelectSingleNode("News/Content");
- Console.WriteLine(node.InnerText);
- //C#读取XML节点method 3
- DataSet ds = new DataSet();
- ds.ReadXml(sFile);
- Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());
- //C#读取XML节点method 4
- XmlTextReader reader = new XmlTextReader(sFile);
- while (reader.Read())
- {
- if (reader.Name == "Content")
- {
- Console.WriteLine("***" + reader.ReadString());
- break;
- }
- }
- reader.Close();
- //C#读取XML节点method 5
- XPathDocument xpdoc = new XPathDocument(sFile);
- XPathNavigator xpnv = xpdoc.CreateNavigator();
- xpnv.MoveToFirstChild();
- xpnv.MoveToFirstChild();
- xpnv.MoveToFirstChild();
- xpnv.MoveToNext();xpnv.MoveToNext();xpnv.MoveToNext();
- Console.WriteLine("pathnavigator:" + xpnv.Value);
- }
- }
我的方法
string newQiChar=DateTime.Now .ToString ("yyMMdd"); string phyPath = AppDomain.CurrentDomain.BaseDirectory; string name = "config.xml"; string configPath = Path.Combine(phyPath, name); SaveConfigXml (configPath,newQi,newQiChar);
/// <summary> /// 保存配置文件 /// </summary> /// <param name="savePath">文件路径</param> /// <param name="qishu">int型期数</param> /// <param name="qishuChar">字符串型期数</param> /// <returns></returns> public static bool SaveConfigXml(string savePath,int qishu,string qishuChar) { bool flag = false; try { XmlDocument doc = new XmlDocument(); bool exists = System.IO.File.Exists(savePath); if (!exists) {//生成 doc.LoadXml("<Configs></Configs>"); //创建根结点 MaxQishu XmlNode root = doc.SelectSingleNode("Configs"); XmlElement MaxQishu = doc.CreateElement("MaxQishu"); MaxQishu.SetAttribute("id", "qishu"); MaxQishu.SetAttribute("value", qishu.ToString()); MaxQishu.SetAttribute("text", qishuChar); root.AppendChild(MaxQishu); doc.Save(savePath); } else {//修改 doc.Load(savePath); XmlElement xe = (XmlElement)doc.SelectSingleNode("Configs/MaxQishu"); if (xe != null) { xe.SetAttribute("id", "qishu"); xe.SetAttribute("value", qishu.ToString()); xe.SetAttribute("text", qishuChar); doc.Save(savePath); //保存 }
} flag = true; } catch (Exception ex) { flag = false; } return flag; }
/// <summary> /// 从配置文件中读取期数 /// </summary> /// <param name="savePath"></param> /// <param name="type">1-int数字类型期数,0-字符串类型期数</param> /// <returns></returns> public static object ReadMaxQishuFromXml(string savePath, int type) { object result = string.Empty; try { XmlDocument doc = new XmlDocument(); bool exists = System.IO.File.Exists(savePath); if (exists) {//存在 doc.Load(savePath); #region /* string xpathPath = ""; if (type == 1) {//数字期数 xpathPath = "Configs/MaxQishu/value"; } else { xpathPath = "Configs/MaxQishu/text"; } XmlNode node = (XmlNode)doc.SelectSingleNode(xpathPath); result = node.InnerText; * */ #endregion XmlElement xe = (XmlElement)doc.SelectSingleNode("Configs/MaxQishu"); string name = xe.Name; string value = xe.Value; if (type == 1) { result = xe.GetAttribute("value"); } else { result = xe.GetAttribute("text"); } }
} catch (Exception ex) { } return result; } }
- C#读取XML节点内容方法实例简析
- c#读取xml文件的节点内容
- C#读取xml特定节点实例
- C#读取xml节点五种方法
- c# 读取xml节点
- c# 读取xml节点 .
- C# 读取XML 节点
- C#读取XML节点
- c#读取xml中特定节点的值(实例)
- C#读取XML内容
- C#读取xml节点数据
- C#读取XML每个节点
- PHP解析XML,读取节点内容
- C#读取XML文档实例
- C#下读取xml节点的数据
- C#中读取xml文件指定节点
- C# xml文件读取节点写入数据
- c#读取xml文件的一个节点
- 连续票号剔除算法
- 中文电码(姓名 电报码 VISA DS160 美国签证)在线查询工具
- linux 提纲一
- C# xml读写与dataset结合
- Opensolaris: 设置静态IP地址
- C#读取XML节点内容方法实例简析
- 学习C语言的回调函数
- 程序进入Android Market(网页或者应用)
- JBPM开发资料整理
- 一个 XSD 实例
- [Asp.net4.0].net 4.0 部署
- POJ 1517 u Calculate e
- 关于Update在不同数据库的使用
- 使用javamail遇到的一系列问题.