日期:2014-05-18 浏览次数:20993 次
public void readXmla() { XmlDocument doc = new XmlDocument(); doc.Load("pic.xml"); XmlNodeList elemList = doc.GetElementsByTagName("img"); foreach (XmlNode xn in elemList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 Console.WriteLine("name=" + xe.GetAttribute("picpath"));//[color=#FF0000]在这里只能把picpath当做属性拿出来,但是无法得到clickurl[/color] [color=#FF0000]//假设我要在这里做判断,由picpath得到他对应的clickurl该怎么做?[/color] if(xe.GetAttribute("picpath")=="中国") { [color=#FF0000]//我在这里想拿到 clickurl的值[/color] } } }
System.Xml.XmlDocument document = new System.Xml.XmlDocument(); document.InnerXml = @" <imgdata> <img picpath=""中国""> <clickurl> 1 </clickurl> </img> <img picpath=""日本""> <clickurl> 2 </clickurl> </img> <img picpath=""美国""> <clickurl> 3 </clickurl> </img> </imgdata> "; System.Xml.XmlNodeList nl = document.SelectNodes(@"//clickurl"); foreach (System.Xml.XmlNode n in nl) { Console.WriteLine(n.ParentNode.Attributes["picpath"].Value); Console.WriteLine(n.InnerText); }
------解决方案--------------------
public void readXmla()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\project\zhangbotest\WindowsApplication1\WindowsApplication1\aa.xml");
XmlNodeList elemList = doc.SelectNodes("//imgdata//img[@picpath='中国']");
foreach (XmlElement e in elemList)
{
e["name"];
e["clickurl"];
}
}