如何遍历xml
xml文件如下,不知怎么遍历出所有值,求教
<?xml version="1.0" encoding="gb2312"?>
<userinfo>
<clas uid="1" uno="101" uname="a" utype="1">
<guid gname="wx" gtype="3" />
<home addr="" mobil="" mail=""/>
<friendm>
<friendm fnum="" fname="" />
</friendm>
<friendwm fnum="" fname=""/>
</clas>
<clas uid="2" uno="102" uname="b" utype="1">
<guid gname="we" gtype="3" />
<home addr="" mobil="" mail=""/>
<friendm>
<friendm fnum="" fname="" />
</friendm>
<friendwm fnum="" fname=""/>
</clas>
<clas uid="3" uno="1011" uname="c" utype="1">
<guid gname="zf" gtype="3" />
<home addr="" mobil="" mail=""/>
<friendm>
<friendm fnum="" fname="" />
</friendm>
<friendwm fnum="" fname=""/>
</clas>
</userinfo>
[解决办法]
把你问题的标题输入google都有答案。如果你明知这一点,那么你无非就是想找个苦力而已了。
[解决办法]
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace ProjectTool
{
/// <summary>
/// xml文件操作类
/// </summary>
class XmlHelper
{
XmlDocument xmlDoc = new XmlDocument();
#region 获取指定目录下所有子节点的值
/// <summary>
/// 获取指定目录下所有子节点的值
/// </summary>
/// <param name="strFilePath">文件路径</param>
/// <param name="nodeDir">节点目录</param>
/// <returns></returns>
public Hashtable GetNodeList(string strFilePath, string nodeDir)
{
Hashtable strNodeList = new Hashtable();
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFilePath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
strNodeList.Add(xe.GetAttribute("id").ToString(), xe.InnerText.Trim());
}
}
catch (Exception)
{
throw;
}
return strNodeList;
}
#endregion
#region 获取指定节点的值
/// <summary>
/// 获取指定节点的值
/// </summary>
/// <param name="strFilePath">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">设置后的值</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <returns></returns>
public string GetNodeValue(string strFilePath, string nodeName, string nodeDir)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFilePath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
value = xe.InnerText.Trim();
break;
}
}
}
catch (Exception exp)
{
throw exp;
}
return value;
}
#endregion
#region 获取指定节点下面对应属性的值
/// <summary>
/// 获取指定节点下面对应属性的值
/// </summary>
/// <param name="strFileName">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeDir">指定节点所在的节点目录</param>
/// <param name="attribute">节点对应的属性名称</param>
/// <returns></returns>
public string GetNodeValue(string strFileName, string nodeName, string nodeDir, string attribute)
{
string value = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList) //遍历所有子节点
{
XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
if (xe.Name == nodeName)
{
//value = xe.InnerText.Trim();
value = (xe).Attributes[attribute].Value;
break;
}
}
}
catch (Exception exp)
{
throw exp;
}
return value;
}
#endregion
}
}
[解决办法]
根据网上的内容改的,不好使的地方帮忙说一下啊
[解决办法]
要取什么值,说清楚点,一般就是用XmlDocument类的SelectNodes方法。
[解决办法]
XElement xmlDoc = XElement.Parse(xmlString);
var queryResults = from element in xmlDoc.Elements("clas")
where element.Element("UID").Value == "3"
select element;
这也是一个方法,查UID=3的
[解决办法]
这种文件,遍历没用,建议使用SelectNodes用XPath表达式查询你需要的节点,放在一个线性链表中,
[解决办法]
你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,
[解决办法]
XmlDocument xd = new XmlDocument();
string xmlpath = Server.MapPath("name.xml");//加载你的xml文件
xd.Load(@xmlpath);
XmlElement xmlRoot = xd.DocumentElement;
XmlNode node = xmlRoot.SelectSingleNode("userinfo");
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.InnerText == "clas")//循环所有的clas
{
if (xn.Attributes.Count > 0)
{
for (int i = 0; i < xn.Attributes.Count; i++)//循环属性
{
if (xn.Attributes[i].Name == "uid")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uno")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uname")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
}
}
}
}
[解决办法]
我13楼发的那段代码,直接就可以用,把xml名字改一下就行,
在有什么问题? 请详细说明
[解决办法]
XmlDocument xd = new XmlDocument();
string xmlpath = Server.MapPath("XMLFile1.xml");//加载你的xml文件
xd.Load(@xmlpath);
XmlElement xmlRoot = xd.DocumentElement;
XmlNode node = xmlRoot.SelectSingleNode("userinfo");
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.Name == "clas")//循环所有的clas
{
if (xn.Attributes.Count > 0)
{
for (int i = 0; i < xn.Attributes.Count; i++)//循环属性
{
if (xn.Attributes[i].Name == "uid")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uno")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uname")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
}
}
}
}
你把userinfo外面加一个root就可以了 例如<root><userinfo> </userinfo></root>
你加好root以后 在把我现在发的代码试试就可以了
另外一种思路
或者你直接从根目录读取也行
[解决办法]
按照我16楼写的不会有什么问题 我已经测试过了
你可以按照另外一种思路 从根目录读取也行的
再有什么问题的话 再回复吧!
[解决办法]
另外一种不用加<root>也可以 直接根目录读取:
XmlDocument xd = new XmlDocument();
string xmlpath = Server.MapPath("XMLFile1.xml");//加载你的xml文件
xd.Load(@xmlpath);
XmlElement xmlRoot = xd.DocumentElement;
XmlNode node = xmlRoot.SelectSingleNode("/userinfo");
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.Name == "clas")//循环所有的clas
{
if (xn.Attributes.Count > 0)
{
for (int i = 0; i < xn.Attributes.Count; i++)//循环属性
{
if (xn.Attributes[i].Name == "uid")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uno")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uname")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
}
}
}
}
[解决办法]
谢谢谢谢谢谢谢谢
[解决办法]
原理是一样的
XmlDocument xd = new XmlDocument();
string xmlpath = Server.MapPath("XMLFile1.xml");//加载你的xml文件
xd.Load(@xmlpath);
XmlElement xmlRoot = xd.DocumentElement;
XmlNode node = xmlRoot.SelectSingleNode("/userinfo");
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.Name == "clas")//循环所有的clas
{
foreach (XmlNode xa in xn.ChildNodes)
{
if (xa.Name == "guid")
{
if (xa.Attributes.Count > 0)
{
for (int i = 0; i < xa.Attributes.Count; i++)//循环属性
{
if (xa.Attributes[i].Name == "gname")
Response.Write("属性值:" + xa.Attributes[i].Value + "</br>");
if (xa.Attributes[i].Name == "gtype")
Response.Write("属性值:" + xa.Attributes[i].Value + "</br>");
}
}
}
}
if (xn.Attributes.Count > 0)
{
for (int i = 0; i < xn.Attributes.Count; i++)//循环属性
{
if (xn.Attributes[i].Name == "uid")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uno")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uname")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
}
}
}
}
我这样处理也行,
另外一种思路: 如果你循环的次数多的话 用递归去处理更方便
------解决方案--------------------
原理是一样的
XmlDocument xd = new XmlDocument();
string xmlpath = Server.MapPath("XMLFile1.xml");//加载你的xml文件
xd.Load(@xmlpath);
XmlElement xmlRoot = xd.DocumentElement;
XmlNode node = xmlRoot.SelectSingleNode("/userinfo");
foreach (XmlNode xn in node.ChildNodes)
{
if (xn.Name == "clas")//循环所有的clas
{
foreach (XmlNode xa in xn.ChildNodes)
{
if (xa.Name == "guid")
{
if (xa.Attributes.Count > 0)
{
for (int i = 0; i < xa.Attributes.Count; i++)//循环属性
{
if (xa.Attributes[i].Name == "gname")
Response.Write("属性值:" + xa.Attributes[i].Value + "</br>");
if (xa.Attributes[i].Name == "gtype")
Response.Write("属性值:" + xa.Attributes[i].Value + "</br>");
}
}
}
}
if (xn.Attributes.Count > 0)
{
for (int i = 0; i < xn.Attributes.Count; i++)//循环属性
{
if (xn.Attributes[i].Name == "uid")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uno")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
if (xn.Attributes[i].Name == "uname")
Response.Write("属性值:" + xn.Attributes[i].Value + "</br>");
}
}
}
}
我这样处理也行,
另外一种思路: 如果你循环的次数多的话 用递归去处理更方便
[解决办法]我觉得你可以去看下XML序列化和反序列化方面的资料,如果你的XML文件中的数据很多的话,用序列化会比你遍历节点快一些
[解决办法]