<xml>
<T>
<Picture>http://abc.com/EC43F1B37F9234A73973F1.gif</Picture>
<T2>
<Text>产品1</RoomType>
<Price>388</MarketPrice>
</T2>
</T>
<T>
<Picture>http://abc.com/2.gif</Picture>
<T2>
<Text>产品2</RoomType>
<Price>388</MarketPrice>
</T2>
</T>
<T>
<Picture>http://abc.com/3.gif</Picture>
<T2>
<Text>产品3</RoomType>
<Price>388</MarketPrice>
</T2>
</T>
</xml>responseDoc.LoadXml(xmlstr);
System.Xml.XmlNodeList nodeOne= responseDoc.SelectNodes("/xml/T");
foreach (System.Xml.XmlNode node in nodeOne)
{
tmepstr = node["Picture"].InnerText;
//System.Xml.XmlNodeList nodeTwo= node.SelectNodes("/T2");
//tmepstr +=nodeTwo["Text"].InnerText;
}上面是数据查看Picture已可取出来,但现在我想取的是T2内的数据,有什么方法能直接取出来吗我试着用 nodeOne.SelectNodes("/T2") 发现没有数据
我需要 <T> 节点取出,再取T2节点,因为需要处理,需要一个一个取,不是全部取出。
循环-每个T2节点,可以再用xpath路径取就可以取子节点了
foreach (XmlNode var in node.ChildNodes)
{
if(var.Name="T2")
{
//这样判断遍历
}
}
foreach (XmlNode var in node.ChildNodes)
{
if(var.Name="T2")
{
//这样判断遍历
}
}
<Price>388</MarketPrice> 这标签不匹配
正如12楼所说的,标签不匹配
修改一下以后代码如下 XmlDocument doc = new XmlDocument();
string xml = @"<xml>
<T>
<Picture>http://abc.com/EC43F1B37F9234A73973F1.gif</Picture>
<T2>
<Text>产品1</Text>
<Price>388</Price>
</T2>
</T>
<T>
<Picture>http://abc.com/2.gif</Picture>
<T2>
<Text>产品2</Text>
<Price>388</Price>
</T2>
</T>
<T>
<Picture>http://abc.com/3.gif</Picture>
<T2>
<Text>产品3</Text>
<Price>388</Price>
</T2>
</T>
</xml>"; doc.LoadXml( xml );
System.Xml.XmlNodeList nodeOne = doc.SelectNodes( "/xml/T" );
foreach ( System.Xml.XmlNode node in nodeOne )
{ //string tmepstr = node["T2"].InnerText;
//System.Xml.XmlNodeList nodeTwo= node.SelectNodes("/T2");
//tmepstr +=nodeTwo["Text"].InnerText;
System.Console.WriteLine( string.Format( "Picture:{0}, Text:{1}, Price:{2}", node.SelectNodes( "Picture" )[0].InnerText, node.SelectNodes( "T2/Text" )[0].InnerText, node.SelectNodes( "T2/Price" )[0].InnerText ) );
}