public void showXML(string path)
{
XmlDocument xd = new XmlDocument();
xd.Load(path);
XmlNodeList xnl = xd.DocumentElement.ChildNodes; foreach (XmlNode xn in xnl)
{
Response.Write(xn.Attributes["name"].Value.ToString()+"<br>");
XmlNode child = xn.FirstChild;
NodeOperate(child);
}
} public void NodeOperate(XmlNode xn1)
{ if (xn1.HasChildNodes == true)
{
Response.Write(xn1.Name + "<br>");
Response.Write("<br>");
XmlNode childNode = xn1.FirstChild; NodeOperate(childNode); }
else
{
Response.Write(xn1.Name + "<br>");
Response.Write(xn1.InnerText);
Response.Write("<br>");
if (xn1.NextSibling != null)
{ NodeOperate(xn1.NextSibling);
}
else
{
while (xn1.NextSibling == null) //遍历到底后移到其父节点的下一个元素
{
xn1 = xn1.ParentNode;
}
NodeOperate(xn1.NextSibling);
} }
}XML文件:
<root>
<person name="张三">
<password>12</password>
<email>[email protected]</email>
<detail>
<hobby>吃</hobby>
<other>无</other>
</detail>
</person>
<person name="李四">
<password>1212</password>
<email>[email protected]</email>
<detail>
<hobby>电脑</hobby>
<other>无</other>
</detail>
</person>
</root>问题是当我运行的时候程序报错,说这句while (xn1.NextSibling == null)未将对象引用设置到对象的实例,不知道怎么解决?
{
XmlDocument xd = new XmlDocument();
xd.Load(path);
XmlNodeList xnl = xd.DocumentElement.ChildNodes; foreach (XmlNode xn in xnl)
{
Response.Write(xn.Attributes["name"].Value.ToString()+"<br>");
XmlNode child = xn.FirstChild;
NodeOperate(child);
}
} public void NodeOperate(XmlNode xn1)
{ if (xn1.HasChildNodes == true)
{
Response.Write(xn1.Name + "<br>");
Response.Write("<br>");
XmlNode childNode = xn1.FirstChild; NodeOperate(childNode); }
else
{
Response.Write(xn1.Name + "<br>");
Response.Write(xn1.InnerText);
Response.Write("<br>");
if (xn1.NextSibling != null)
{ NodeOperate(xn1.NextSibling);
}
else
{
while (xn1.NextSibling == null) //遍历到底后移到其父节点的下一个元素
{
xn1 = xn1.ParentNode;
}
NodeOperate(xn1.NextSibling);
} }
}XML文件:
<root>
<person name="张三">
<password>12</password>
<email>[email protected]</email>
<detail>
<hobby>吃</hobby>
<other>无</other>
</detail>
</person>
<person name="李四">
<password>1212</password>
<email>[email protected]</email>
<detail>
<hobby>电脑</hobby>
<other>无</other>
</detail>
</person>
</root>问题是当我运行的时候程序报错,说这句while (xn1.NextSibling == null)未将对象引用设置到对象的实例,不知道怎么解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货