{
string id = Convert.ToString(Request.QueryString["id"]);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int j = 0; j < xnl.Count; j++)
{
XmlNode xn = xnl.Item(j);
XmlElement xe = (XmlElement)xnl.Item(j); //这个报错
if(xe["ID"].InnerText == id.ToString())
{
root.RemoveChild(xe);
break;
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
}
求高手解决
9 个解决方案
#1
用 as
#2
修改为:
for (int j = 0; j < xnl.Count; j++)
{
XmlNode xn = xnl[j];
if(xe["ID"].InnerText == id.ToString())
{
root.RemoveChild(xn);
break;
}
}
for (int j = 0; j < xnl.Count; j++)
{
XmlNode xn = xnl[j];
if(xe["ID"].InnerText == id.ToString())
{
root.RemoveChild(xn);
break;
}
}
#3
xe怎么来的。。。加上那句还是报那句错
#4
XmlElement xe =xnl.Item(j) as XmlElement ; ??/这样貌似也不行
#5
if(xn is XmlElement){
...
}else if(xn is XmlComment){
...
#6
我这问题还是出在添加上,原先XML里面有的图片都能删除,我自己添加的图片就不能删除
#7
Random ran = new Random();
int id = ran.Next(1000, 9999);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");//查找<bcaster>
XmlElement xe1 = xmlDoc.CreateElement("image");//创建一个<item>节点
XmlElement xesub3 = xmlDoc.CreateElement("thumbnail"); //添加person节点的里的节点
xesub3.InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe1.AppendChild(xesub3);
XmlElement xesub1 = xmlDoc.CreateElement("pic"); //添加person节点的里的节点
xesub1.InnerText = Convert.ToString(Request.Form["pic"]);
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("url");
xesub2.InnerText = Convert.ToString(Request.Form["url"]);
xe1.AppendChild(xesub2);
XmlElement xesub4 = xmlDoc.CreateElement("ID");
xesub4.InnerText = id.ToString();
xe1.AppendChild(xesub4);
//XmlElement xesub5 = xmlDoc.CreateElement("text_low");
//xesub5.InnerText = Convert.ToString(Request.Form["text_low"]);
//xe1.AppendChild(xesub5);
root.AppendChild(xe1);//添加到<bcaster>节点中
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
else if (action == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.Show(id.ToString());
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
if (Convert.ToString(Request.QueryString["result"]) == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.ShowAndGo(id.ToString() + "aaa", "flash-edit.aspx?result=edit&id=8294");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
这是编辑的代码,显示那边删除就报那个错
int id = ran.Next(1000, 9999);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");//查找<bcaster>
XmlElement xe1 = xmlDoc.CreateElement("image");//创建一个<item>节点
XmlElement xesub3 = xmlDoc.CreateElement("thumbnail"); //添加person节点的里的节点
xesub3.InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe1.AppendChild(xesub3);
XmlElement xesub1 = xmlDoc.CreateElement("pic"); //添加person节点的里的节点
xesub1.InnerText = Convert.ToString(Request.Form["pic"]);
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("url");
xesub2.InnerText = Convert.ToString(Request.Form["url"]);
xe1.AppendChild(xesub2);
XmlElement xesub4 = xmlDoc.CreateElement("ID");
xesub4.InnerText = id.ToString();
xe1.AppendChild(xesub4);
//XmlElement xesub5 = xmlDoc.CreateElement("text_low");
//xesub5.InnerText = Convert.ToString(Request.Form["text_low"]);
//xe1.AppendChild(xesub5);
root.AppendChild(xe1);//添加到<bcaster>节点中
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
else if (action == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.Show(id.ToString());
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
if (Convert.ToString(Request.QueryString["result"]) == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.ShowAndGo(id.ToString() + "aaa", "flash-edit.aspx?result=edit&id=8294");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
这是编辑的代码,显示那边删除就报那个错
#8
#1
用 as
#2
修改为:
for (int j = 0; j < xnl.Count; j++)
{
XmlNode xn = xnl[j];
if(xe["ID"].InnerText == id.ToString())
{
root.RemoveChild(xn);
break;
}
}
for (int j = 0; j < xnl.Count; j++)
{
XmlNode xn = xnl[j];
if(xe["ID"].InnerText == id.ToString())
{
root.RemoveChild(xn);
break;
}
}
#3
xe怎么来的。。。加上那句还是报那句错
#4
XmlElement xe =xnl.Item(j) as XmlElement ; ??/这样貌似也不行
#5
if(xn is XmlElement){
...
}else if(xn is XmlComment){
...
#6
我这问题还是出在添加上,原先XML里面有的图片都能删除,我自己添加的图片就不能删除
#7
Random ran = new Random();
int id = ran.Next(1000, 9999);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");//查找<bcaster>
XmlElement xe1 = xmlDoc.CreateElement("image");//创建一个<item>节点
XmlElement xesub3 = xmlDoc.CreateElement("thumbnail"); //添加person节点的里的节点
xesub3.InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe1.AppendChild(xesub3);
XmlElement xesub1 = xmlDoc.CreateElement("pic"); //添加person节点的里的节点
xesub1.InnerText = Convert.ToString(Request.Form["pic"]);
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("url");
xesub2.InnerText = Convert.ToString(Request.Form["url"]);
xe1.AppendChild(xesub2);
XmlElement xesub4 = xmlDoc.CreateElement("ID");
xesub4.InnerText = id.ToString();
xe1.AppendChild(xesub4);
//XmlElement xesub5 = xmlDoc.CreateElement("text_low");
//xesub5.InnerText = Convert.ToString(Request.Form["text_low"]);
//xe1.AppendChild(xesub5);
root.AppendChild(xe1);//添加到<bcaster>节点中
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
else if (action == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.Show(id.ToString());
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
if (Convert.ToString(Request.QueryString["result"]) == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.ShowAndGo(id.ToString() + "aaa", "flash-edit.aspx?result=edit&id=8294");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
这是编辑的代码,显示那边删除就报那个错
int id = ran.Next(1000, 9999);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");//查找<bcaster>
XmlElement xe1 = xmlDoc.CreateElement("image");//创建一个<item>节点
XmlElement xesub3 = xmlDoc.CreateElement("thumbnail"); //添加person节点的里的节点
xesub3.InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe1.AppendChild(xesub3);
XmlElement xesub1 = xmlDoc.CreateElement("pic"); //添加person节点的里的节点
xesub1.InnerText = Convert.ToString(Request.Form["pic"]);
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("url");
xesub2.InnerText = Convert.ToString(Request.Form["url"]);
xe1.AppendChild(xesub2);
XmlElement xesub4 = xmlDoc.CreateElement("ID");
xesub4.InnerText = id.ToString();
xe1.AppendChild(xesub4);
//XmlElement xesub5 = xmlDoc.CreateElement("text_low");
//xesub5.InnerText = Convert.ToString(Request.Form["text_low"]);
//xe1.AppendChild(xesub5);
root.AppendChild(xe1);//添加到<bcaster>节点中
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
else if (action == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.Show(id.ToString());
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
if (Convert.ToString(Request.QueryString["result"]) == "edit")
{
string id = Convert.ToString(Request.QueryString["id"]);
Alert.ShowAndGo(id.ToString() + "aaa", "flash-edit.aspx?result=edit&id=8294");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../Ch/swf/pic.xml"));
XmlNode root = xmlDoc.SelectSingleNode("folio");
XmlNodeList xnl = xmlDoc.SelectSingleNode("folio").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe["ID"].InnerText == id.ToString())
{
xe["thumbnail"].InnerText = Convert.ToString(Request.Form["thumbnail"]);
xe["pic"].InnerText = Convert.ToString(Request.Form["pic"]);
xe["url"].InnerText = Convert.ToString(Request.Form["url"]);
//xe.SetAttribute("itemtitle", "" + this.FlashText.Value + "");
}
}
xmlDoc.Save(Server.MapPath("../Ch/swf/pic.xml"));
Alert.GoParent("flash-list.aspx");
}
这是编辑的代码,显示那边删除就报那个错