#读取XML节点内容方法实例入手让我们来看看C#读取XML节点的实现:
- using System;
- using System.Xml;
- using System.Xml.XPath;
- using System.Data;
-
- 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;
}
}