xml文件格式如下<?xml version="1.0" encoding="GB2312" ?>
<allproducts version="1.0">
<products>
<product name="prodId" value="28127" />
<product name="prodContent" value="5" />
<product name="prodPrice" value="5" />
</products>
<products>
<product name="prodId" value="10335" />
<product name="prodContent" value="5" />
<product name="prodPrice" value="4.95" />
</products>
<products>
<product name="prodId" value="28328" />
<product name="prodContent" value="5" />
<product name="prodPrice" value="5" />
</products>
</allproducts>遍历代码如下 XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("E:\\1.xml");
XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;
XmlNodeList list = xmldoc.SelectSingleNode("/allproducts/products").ChildNodes;
//XmlNode list = xmldoc.SelectSingleNode("/allproducts/products");
foreach( XmlNode xn1 in list2 )
{
foreach (XmlNode xn in list)
{
//遍历子节点
XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
if (xe.GetAttribute("name") == "prodId")
{
M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
}
if(xe.GetAttribute("name") == "prodContent")
{
M_prodContent = decimal.Parse(xe.GetAttribute("value"));
} }
}这段代码的问题就是总是取第一个<products></products>中的值,跳不到下面两个,这是什么问题呢?请高手赐教,谢谢!
<allproducts version="1.0">
<products>
<product name="prodId" value="28127" />
<product name="prodContent" value="5" />
<product name="prodPrice" value="5" />
</products>
<products>
<product name="prodId" value="10335" />
<product name="prodContent" value="5" />
<product name="prodPrice" value="4.95" />
</products>
<products>
<product name="prodId" value="28328" />
<product name="prodContent" value="5" />
<product name="prodPrice" value="5" />
</products>
</allproducts>遍历代码如下 XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("E:\\1.xml");
XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;
XmlNodeList list = xmldoc.SelectSingleNode("/allproducts/products").ChildNodes;
//XmlNode list = xmldoc.SelectSingleNode("/allproducts/products");
foreach( XmlNode xn1 in list2 )
{
foreach (XmlNode xn in list)
{
//遍历子节点
XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
if (xe.GetAttribute("name") == "prodId")
{
M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
}
if(xe.GetAttribute("name") == "prodContent")
{
M_prodContent = decimal.Parse(xe.GetAttribute("value"));
} }
}这段代码的问题就是总是取第一个<products></products>中的值,跳不到下面两个,这是什么问题呢?请高手赐教,谢谢!
xmldoc.Load("E:\\1.xml");
XmlNodeList list = xmldoc.SelectSingleNode("//products").ChildNodes; foreach (XmlNode xn in list)
{
//遍历子节点
XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
if (xe.GetAttribute("name") == "prodId")
{
M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
}
if(xe.GetAttribute("name") == "prodContent")
{
M_prodContent = decimal.Parse(xe.GetAttribute("value"));
} }
改成 XmlNodeList nodelist = xmldoc.GetElementsByTagName("products");
就可以了
XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes; foreach (XmlNode node in nodelist)
{
foreach (XmlNode xn in node.ChildNodes)
{
//遍历子节点
XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
if (xe.GetAttribute("name") == "prodId")
{
M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
}
if(xe.GetAttribute("name") == "prodContent")
{
M_prodContent = decimal.Parse(xe.GetAttribute("value"));
} }
}刚刚说错了,仔细看了你的错误,应该这么改。。
不知道各位大虾还有更简单的方法没有呢XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;
foreach (XmlNode xn1 in list2)
{
XmlElement xe = (XmlElement)xn1;
XmlNodeList list = xe.ChildNodes;
foreach (XmlNode xn in list)
{
//遍历子节点
XmlElement xe2 = (XmlElement)xn;//将子节点类型转换为xmlelement类型
if (xe2.GetAttribute("name") == "prodId")
{
M_prodId = Convert.ToInt32(xe2.GetAttribute("value"));
}
if (xe2.GetAttribute("name") == "prodContent")
{
M_prodContent = decimal.Parse(xe2.GetAttribute("value"));
} }
}