c#读取 XML 的问题 可以使用System.XML命名空间中的类如:System.Xml.XmlDocument类的LoadXml方法来从指定的字符串加载 XML 文档。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可尝试汇入 using System.Xml 后,自己用 .net 内建的 读、写 XML 的 API 去写一支简单的文件读取、解析函数。using System.Xml;public string ReadXML(string strUser_No, string strFunc_No){ string str_SFUNC_NO = null; // 读取位于 ~/App_Data/xml 目录中的 XML XmlDocument doc = new XmlDocument(); string FilePath = null; FilePath = HttpContext.Current.Server.MapPath("~/App_Data/xml/" + strUser_No + ".xml"); doc.Load(FilePath); //doc.Load(Server.MapPath("../../App_Data/xml/" + strUser_No + ".xml")); XmlNodeList nodeList; XmlNode root = doc.DocumentElement; // 既有的 XML 档里,有几个「USER_NO」Tag,待会循环就跑几次 nodeList = root.SelectNodes("//NewDataSet/DataTable2/USER_NO/text()"); // 跑循环,把 App_Data/xml 目录中,存储有关这支程序其权限的 XML 文件,把内容全都读出来 for (int i = 0; i <= nodeList.Count - 1; i++) { if (root.SelectNodes("//NewDataSet/DataTable2/FUNC_NO/text()").Item(i).Value == strFunc_No.Trim()) { // str_SFUNC_NO = root.SelectNodes("//NewDataSet/DataTable2/SFUNC_NO/text()").Item(i).Value; break; // 跳出 loop } } return str_SFUNC_NO;} using System.Xml;public string ReadXML(string strUser_No, string strFunc_No){ string str_SFUNC_NO = null; // 读取位于 ~/App_Data/xml 目录中的 XML XmlDocument doc = new XmlDocument(); string FilePath = null; FilePath = HttpContext.Current.Server.MapPath("~/App_Data/xml/" + strUser_No + ".xml"); doc.Load(FilePath); //doc.Load(Server.MapPath("../../App_Data/xml/" + strUser_No + ".xml")); XmlNodeList nodeList; XmlNode root = doc.DocumentElement; // 既有的 XML 档里,有几个「USER_NO」Tag,待会循环就跑几次 nodeList = root.SelectNodes("//NewDataSet/DataTable2/USER_NO/text()"); // 跑循环,把 App_Data/xml 目录中,存储有关这支程序其权限的 XML 文件,把内容全都读出来 for (int i = 0; i <= nodeList.Count - 1; i++) { if (root.SelectNodes("//NewDataSet/DataTable2/FUNC_NO/text()").Item(i).Value == strFunc_No.Trim()) { // str_SFUNC_NO = root.SelectNodes("//NewDataSet/DataTable2/SFUNC_NO/text()").Item(i).Value; break; // 跳出 loop } } return str_SFUNC_NO;} using System.Xml; XmlDocument doc = new XmlDocument(); string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TEST.exe.config"; //此中 TEST 为你程序名称 doc.Load(strFileName); XmlNodeList nodes = doc.GetElementsByTagName("General"); //找到名称为General所有元素 for (int i = 0; i < nodes[0].Attributes.Count; i++) { try { Console.WriteLine(nodes[0].Attributes[i].Name + " " + nodes[0].Attributes[i].Value); //前一个为 属性名称, 后一个为 键值 } catch (Exception) { continue; } } http://blog.csdn.net/llwinnner/archive/2009/03/18/4002739.aspx你看看吧 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"C:\Documents and Settings\Administrator\桌面\作业\Lesson5XML\Lesson5XML\XMLFile1.xml"); XmlElement xmlEle = xmlDoc.DocumentElement; //MessageBox.Show(xmlEle.Name); TreeNode node = null; foreach(XmlNode xmlNode in xmlEle.ChildNodes) { switch(xmlNode.Name) { case"name": node = new TreeNode(); //treeview的根节点 node.Text = xmlNode.InnerText; //提取中间值 this.treeView1.Nodes.Add(node); break; case"age": node.Nodes.Add(xmlNode.InnerText); break; case"hobby": node.Nodes.Add(xmlNode.InnerText); break; } }循环读取 最简单的方式就是使用Linq 2 XML了。上MSDN看看XDocument的用法。 把查询结果生成报表 新手求一个排坐位算法 [Network] 如何实现双向通信? 怎么在任意电脑上运行托管程序? 捕获buffer是否有指定字符串.... checkbox和页面不刷新 如何实现根据屏幕分辨率来适应让程序界面总是全屏呢(winform) 怎么取得指定的节点信息? vs2010 创建ESRIStandardMapApplication2 有错误 一个简单得画图问题 反射_获取泛型 attribute c#.net做中国象棋问题
{
string str_SFUNC_NO = null; // 读取位于 ~/App_Data/xml 目录中的 XML
XmlDocument doc = new XmlDocument();
string FilePath = null;
FilePath = HttpContext.Current.Server.MapPath("~/App_Data/xml/" + strUser_No + ".xml");
doc.Load(FilePath);
//doc.Load(Server.MapPath("../../App_Data/xml/" + strUser_No + ".xml")); XmlNodeList nodeList;
XmlNode root = doc.DocumentElement; // 既有的 XML 档里,有几个「USER_NO」Tag,待会循环就跑几次
nodeList = root.SelectNodes("//NewDataSet/DataTable2/USER_NO/text()"); // 跑循环,把 App_Data/xml 目录中,存储有关这支程序其权限的 XML 文件,把内容全都读出来
for (int i = 0; i <= nodeList.Count - 1; i++)
{
if (root.SelectNodes("//NewDataSet/DataTable2/FUNC_NO/text()").Item(i).Value == strFunc_No.Trim())
{
//
str_SFUNC_NO = root.SelectNodes("//NewDataSet/DataTable2/SFUNC_NO/text()").Item(i).Value; break; // 跳出 loop
}
} return str_SFUNC_NO;
}
{
string str_SFUNC_NO = null; // 读取位于 ~/App_Data/xml 目录中的 XML
XmlDocument doc = new XmlDocument();
string FilePath = null;
FilePath = HttpContext.Current.Server.MapPath("~/App_Data/xml/" + strUser_No + ".xml");
doc.Load(FilePath);
//doc.Load(Server.MapPath("../../App_Data/xml/" + strUser_No + ".xml")); XmlNodeList nodeList;
XmlNode root = doc.DocumentElement; // 既有的 XML 档里,有几个「USER_NO」Tag,待会循环就跑几次
nodeList = root.SelectNodes("//NewDataSet/DataTable2/USER_NO/text()"); // 跑循环,把 App_Data/xml 目录中,存储有关这支程序其权限的 XML 文件,把内容全都读出来
for (int i = 0; i <= nodeList.Count - 1; i++)
{
if (root.SelectNodes("//NewDataSet/DataTable2/FUNC_NO/text()").Item(i).Value == strFunc_No.Trim())
{
//
str_SFUNC_NO = root.SelectNodes("//NewDataSet/DataTable2/SFUNC_NO/text()").Item(i).Value; break; // 跳出 loop
}
} return str_SFUNC_NO;
}
string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TEST.exe.config";
//此中 TEST 为你程序名称
doc.Load(strFileName); XmlNodeList nodes = doc.GetElementsByTagName("General"); //找到名称为General所有元素
for (int i = 0; i < nodes[0].Attributes.Count; i++)
{
try
{
Console.WriteLine(nodes[0].Attributes[i].Name + " " + nodes[0].Attributes[i].Value);
//前一个为 属性名称, 后一个为 键值
}
catch (Exception)
{
continue;
}
}
你看看吧
xmlDoc.Load(@"C:\Documents and Settings\Administrator\桌面\作业\Lesson5XML\Lesson5XML\XMLFile1.xml"); XmlElement xmlEle = xmlDoc.DocumentElement;
//MessageBox.Show(xmlEle.Name);
TreeNode node = null;
foreach(XmlNode xmlNode in xmlEle.ChildNodes)
{
switch(xmlNode.Name)
{
case"name":
node = new TreeNode(); //treeview的根节点
node.Text = xmlNode.InnerText; //提取中间值
this.treeView1.Nodes.Add(node);
break;
case"age":
node.Nodes.Add(xmlNode.InnerText);
break;
case"hobby":
node.Nodes.Add(xmlNode.InnerText);
break;
}
}
循环读取
上MSDN看看XDocument的用法。