加载节点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class LoadXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xml = "<order id='001'>";
xml +="<product>";
xml +="<name>手机</name>";
xml +="<quantity>10</quantity>";
xml +="<price>2000</price>";
xml +="</product>";
xml += "</order>";
XmlDocument xmlDoc = new XmlDocument();
//使用变量xml中保存在XML字符串信息
xmlDoc.LoadXml(xml);
//显示根元素 使用Response对象的Write()方法输出 所以特殊标记需要使用实体引用
string output="<"+xmlDoc.DocumentElement.Name;
output = " " + xmlDoc.DocumentElement.Attributes[0].Name + "="" + xmlDoc.DocumentElement.Attributes[0].Value + "">";
//获取根元素的第一个子元素<product>的所有子节点
XmlNodeList nodes = xmlDoc.DocumentElement.FirstChild.ChildNodes;
//output += "<" + xmlDoc.GetElementsByTagName("order") + ">";
var node = xmlDoc.GetElementsByTagName("order");
//output += "<" + node.ParentNode.Name + ">";
for (int i = 0; i <node.Count; i++)
{
output += "<" + node[i].Name;
output +=" "+node[i].Attributes[0].Name+"=""+node[i].Attributes[0].Value+""";
output +="><br/>";
for (var j = 0; j < node[i].ChildNodes.Count; j++)
{
output += " ";
output += "<" + node[i].ChildNodes[j].Name + ">";
output += node[i].ChildNodes[j].ChildNodes[0].Value;
output += "</" + node[i].ChildNodes[j].Name + ">";
output += "<br/>";
}
//output += "<" + node.ChildNodes[i].FirstChild.ChildNodes[0].Name + ">" + "<br/>";
//output += "</" + node.ChildNodes[i].Name + ">" + "<br/>";
}
Response.Write(output);
}
}