读书人

怎么遍历xml

发布时间: 2013-01-18 10:22:42 作者: rapoo

如何遍历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简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,
[解决办法]
引用:
引用:你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,

这个xml有很多clas节点,我想遍历取出里面的所有属性的值;
就是想这样:找到第一个clas,取出其下的所有值,然后再下一个clas,直到找完、取完。

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>");
}
}
}
}
[解决办法]

引用:
引用:你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,

这个xml有很多clas节点,我想遍历取出里面的所有属性的值;
就是想这样:找到第一个clas,取出其下的所有值,然后再下一个clas,直到找完、取完。

我13楼发的那段代码,直接就可以用,把xml名字改一下就行,
在有什么问题? 请详细说明
[解决办法]
引用:

引用:引用:
引用:你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,

这个xml有很多clas节点,我想遍历取出里面的所有属性的值;
就是想这样:找到第一个clas,取出其下的所有值,然后再下一个c……

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以后 在把我现在发的代码试试就可以了
另外一种思路
或者你直接从根目录读取也行
[解决办法]


引用:
引用:引用:
引用:你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,

这个xml有很多clas节点,我想遍历取出里面的所有属性的值;
就是想这样:找到第一个clas,取出其下的所有值,然后再下一个c……

按照我16楼写的不会有什么问题 我已经测试过了
你可以按照另外一种思路 从根目录读取也行的
再有什么问题的话 再回复吧!
[解决办法]
引用:
引用:引用:
引用:你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,

这个xml有很多clas节点,我想遍历取出里面的所有属性的值;
就是想这样:找到第一个clas,取出其下的所有值,然后再下一个c……

另外一种不用加<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>");


}
}
}
}
[解决办法]
谢谢谢谢谢谢谢谢
[解决办法]

引用:
引用:引用:
引用:引用:
引用:你把你的xml简化一下 说明你要取什么值?我们才可以提供帮助
你这一大堆放上去了 说都没说清,

这个xml有很多clas节点,我想遍历……

原理是一样的
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有很多clas节点,我想遍历……

原理是一样的
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序列化和反序列化方面的资料,如果你的XML文件中的数据很多的话,用序列化会比你遍历节点快一些
[解决办法]


[解决办法]
引用:
引用:或者如何进行反序列化呢
xml在vc中很好操作,c#刚用啊



22#楼的处理完全可以解决你的问题 同时读取 clas、uinfo下的值

反序列化的思路:
如果要是用反序列化操作的话 还得处理成字典 最后处理成table形式的数据集合
我还是建议你先去了解了解 反序化的操作
我不能一股脑的把代码全给你贴处理吧!再说我也没时间去从头写代码
怎么遍历xml
[解决办法]
用dataset也是一个方法的


string xml = @"<userinfo>
<clas uid=""1"">
<uinfo uno=""101"" uname=""a"" utype=""1"" addr="""" mobil="""" mail="""" fruum=""3""/>
</clas>
<clas uid=""2"">
<uinfo uno=""102"" uname=""b"" utype=""1"" addr="""" mobil="""" mail="""" fruum=""4""/>
</clas>
<clas uid=""3"">
<uinfo uno=""103"" uname=""c"" utype=""1"" addr="""" mobil="""" mail="""" fruum=""5""/>
</clas>

</userinfo> ";
DataSet ds = new DataSet();
using (System.IO.StringReader reader = new System.IO.StringReader(xml))
{
try
{
System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(reader);
ds.ReadXml(xmlReader);
xmlReader.Close();
}
finally
{
reader.Close();
}
}

读书人网 >C#

热点推荐