xml的遍历
发布网友
发布时间:2022-05-25 11:58
我来回答
共2个回答
热心网友
时间:2023-10-24 23:53
首先将XML加载进来
private void Form1_Load(object sender, EventArgs e)
{
doc.Load(XML的文件名称);
}
//按钮的事件
private void button4_Click(object sender, EventArgs e)
{
string str ="";//放遍历之后所有的东西
XmlNodeList xmllist = doc.SelectSingleNode("grandpa").ChildNodes;
for (int i = 0; i < xmllist.Count; i++)
{
XmlElement xel = xmllist[i] as XmlElement;
str += xel.GetAttribute ("name"),
XmlNodeList xnlist = xel.ChildNodes;
foreach (XmlNode xmlnode in xnlist)
{
str += xnlist.GetAttribute("name")
}
}
doc.Save("XML的名字");
}
热心网友
时间:2023-10-24 23:53
System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
XmlDoc.Load(Server.MapPath("XMLFile1.xml"));
System.Xml.XmlNodeList nodes = XmlDoc.GetElementsByTagName("*");
foreach (System.Xml.XmlNode n in nodes)
{
if (n.Attributes["name"] != null)
{
Response.Write(n.Attributes["name"].Value + "<br />");
}
}