使用XmlDocument中的XmlElement遍历xml文档
2010-06-18 14:45
633 查看
xml文件各个层次的节点名称都一样,所以不能使用DataSet.ReadXml来加载,必须使用XmlDocument对象:
代码
string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
str += "<item>";
XmlDocument xmldc = new XmlDocument();
xmldc.Load(Server.MapPath("XMLFile.xml"));
XmlElement el = xmldc.DocumentElement;
for (int i = 0; i < el.ChildNodes.Count; i++)
{
XmlNode nodeProvince = el.ChildNodes[i];
string province = nodeProvince.Attributes["nodename"].Value;
string provinceID = nodeProvince.Attributes["nodeid"].Value;
str += "<province name=\"" + province + "\" id=\"" + provinceID + "\">";
for (int j = 0; j < el.ChildNodes[i].ChildNodes.Count; j++)
{
XmlNode nodeCity = el.ChildNodes[i].ChildNodes[j];
string city = nodeCity.Attributes["nodename"].Value;
string cityID = nodeCity.Attributes["nodeid"].Value;
str += "<city name=\"" + city + "\" id=\"" + cityID + "\" provinceID=\"" + provinceID + "\">";
for (int k = 0; k < el.ChildNodes[i].ChildNodes[j].ChildNodes.Count; k++)
{
XmlNode nodedistrinct = el.ChildNodes[i].ChildNodes[j].ChildNodes[k];
string distrinct = nodedistrinct.Attributes["nodename"].Value;
string distrinctID = nodedistrinct.Attributes["nodeid"].Value;
str += "<distrinct name=\"" + distrinct + "\" distrinctID=\"" + distrinctID + "\" cityID=\"" + cityID + "\" provinceID=\"" + provinceID + "\" />";
}
str += "</city>";
}
str += "</province>";
}
str += "</item>";
Response.Write(str);
代码
string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
str += "<item>";
XmlDocument xmldc = new XmlDocument();
xmldc.Load(Server.MapPath("XMLFile.xml"));
XmlElement el = xmldc.DocumentElement;
for (int i = 0; i < el.ChildNodes.Count; i++)
{
XmlNode nodeProvince = el.ChildNodes[i];
string province = nodeProvince.Attributes["nodename"].Value;
string provinceID = nodeProvince.Attributes["nodeid"].Value;
str += "<province name=\"" + province + "\" id=\"" + provinceID + "\">";
for (int j = 0; j < el.ChildNodes[i].ChildNodes.Count; j++)
{
XmlNode nodeCity = el.ChildNodes[i].ChildNodes[j];
string city = nodeCity.Attributes["nodename"].Value;
string cityID = nodeCity.Attributes["nodeid"].Value;
str += "<city name=\"" + city + "\" id=\"" + cityID + "\" provinceID=\"" + provinceID + "\">";
for (int k = 0; k < el.ChildNodes[i].ChildNodes[j].ChildNodes.Count; k++)
{
XmlNode nodedistrinct = el.ChildNodes[i].ChildNodes[j].ChildNodes[k];
string distrinct = nodedistrinct.Attributes["nodename"].Value;
string distrinctID = nodedistrinct.Attributes["nodeid"].Value;
str += "<distrinct name=\"" + distrinct + "\" distrinctID=\"" + distrinctID + "\" cityID=\"" + cityID + "\" provinceID=\"" + provinceID + "\" />";
}
str += "</city>";
}
str += "</province>";
}
str += "</item>";
Response.Write(str);
相关文章推荐
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用sp_xml_preparedocument处理XML文档的方法
- 将一段符合xml标准的string转换成xml文档中的element对象(使用dom4j)
- python 6-4 如何构建xml文档使用标准库中的xml.etree.ElementTree 构建ElementTree,使用write方法写入文档
- XmlDocument加载有Xmlns的xml文档,使用Xpath
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument.SelectNodes遍历xml元素遇到的一个XPathException
- C# 使用XmlDocument类对XML文档进行操作
- 使用XmlDataDocument将数据存储到XML文档
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用sp_xml_preparedocument处理XML文档的方法
- 使用dom4j遍历xml 文档快速入门
- 使用XmlDocument创建XML文档及增加删除更新节点
- XML 文档对象模型----加载和使用XmlDocument (w3c DOM)
- 学习笔记之Document Object Model(1)——如何使用document获得文档元素(element对象)
- sp_xml_preparedocument _使用 处理XML文档
- python 6-3 如何解析简单的xml文档 使用标准库中的xml.etree.ElementTree,其中parse函数可以解析xml文档
- 使用sp_xml_preparedocument处理XML文档的方法
- java使用org.w3c.dom解析XML文档,创建、增删查改,保存,读取,遍历元素等操作