xml文件;<ClassName>
<ClassName title="123">
<ClassName title="456"/>
</ClassName>
</ClassName>在遍历时找到XML中title属性的值等于456时,就在此节点下追加一个子节点789
如果已存在789时就提示已经存在此节点结果如下:
<ClassName>
<ClassName title="123">
<ClassName title="456">
<ClassName title="789"/>
</ClassName>
</ClassName>
</ClassName>有人能帮我写一下吗,我定的总不对:代码如下:
public void Class_AddName(string pathfile, string prClass, string classname)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
//载入文件
try
{
xmlDoc.Load(pathfile);
}
catch (Exception ex)
{
Console.WriteLine("文件载入失败", ex.ToString());
} //判断所属目录
if (prClass =="作为一级目录")
{ XmlElement classn = xmlDoc.CreateElement("ClassName");
XmlAttribute xmlattrib=xmlDoc.CreateAttribute("title");
xmlattrib.InnerText=classname;
classn.SetAttributeNode(xmlattrib);
xmlDoc.DocumentElement.AppendChild(classn); //保存文档
xmlDoc.Save(pathfile);
}
else
{
XmlNodeList xmlnodeList = xmlDoc.SelectSingleNode("ClassName").ChildNodes;
//遍历节点,查找是否有和classname名称相同的节点
foreach (XmlNode xn in xmlnodeList)//开始
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("title") == classname)
{
HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
HttpContext.Current.Response.Write("alert(\"对不起,已有相同类名\")" + "\n</script>");
}
else
{
if (xe.GetAttributeNode("title").Value == prClass)
{
XmlElement classn = xmlDoc.CreateElement("ClassName");
XmlAttribute xmlattrib = xmlDoc.CreateAttribute("title");
xmlattrib.InnerText = classname;
classn.SetAttributeNode(xmlattrib);
xe.AppendChild(classn);
}
else
{
HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
HttpContext.Current.Response.Write("alert(\"对不起,没有找到相应的主分类\")" + "\n</script>");
}
//保存文档
xmlDoc.Save(pathfile);
}
}
}
}
<ClassName title="123">
<ClassName title="456"/>
</ClassName>
</ClassName>在遍历时找到XML中title属性的值等于456时,就在此节点下追加一个子节点789
如果已存在789时就提示已经存在此节点结果如下:
<ClassName>
<ClassName title="123">
<ClassName title="456">
<ClassName title="789"/>
</ClassName>
</ClassName>
</ClassName>有人能帮我写一下吗,我定的总不对:代码如下:
public void Class_AddName(string pathfile, string prClass, string classname)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
//载入文件
try
{
xmlDoc.Load(pathfile);
}
catch (Exception ex)
{
Console.WriteLine("文件载入失败", ex.ToString());
} //判断所属目录
if (prClass =="作为一级目录")
{ XmlElement classn = xmlDoc.CreateElement("ClassName");
XmlAttribute xmlattrib=xmlDoc.CreateAttribute("title");
xmlattrib.InnerText=classname;
classn.SetAttributeNode(xmlattrib);
xmlDoc.DocumentElement.AppendChild(classn); //保存文档
xmlDoc.Save(pathfile);
}
else
{
XmlNodeList xmlnodeList = xmlDoc.SelectSingleNode("ClassName").ChildNodes;
//遍历节点,查找是否有和classname名称相同的节点
foreach (XmlNode xn in xmlnodeList)//开始
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("title") == classname)
{
HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
HttpContext.Current.Response.Write("alert(\"对不起,已有相同类名\")" + "\n</script>");
}
else
{
if (xe.GetAttributeNode("title").Value == prClass)
{
XmlElement classn = xmlDoc.CreateElement("ClassName");
XmlAttribute xmlattrib = xmlDoc.CreateAttribute("title");
xmlattrib.InnerText = classname;
classn.SetAttributeNode(xmlattrib);
xe.AppendChild(classn);
}
else
{
HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
HttpContext.Current.Response.Write("alert(\"对不起,没有找到相应的主分类\")" + "\n</script>");
}
//保存文档
xmlDoc.Save(pathfile);
}
}
}
}
解决方案 »
- C# 电子书 出现的怪事!
- winform 网格线背景
- 怎样在新建的windows应用程序中添加一个数据库呢
- 匹配的简单问题
- 高人请进~C#利用远程桌面 弹出服务器上面的移动硬盘?
- 很简单的SQL语句,数据库运行成功,程序得不到
- 如何设置System.Windows.Forms.ToolBar上的ToolBarButton的背景色
- 100分讨论:用户控件如AspNetMenu、TreeView等,还有水晶报表能打包到程序(WebApp) 中吗
- 求助!!如何通过正则表达式获取相关控件的ID!
- DataGridViewComboBoxColumn 列值为空
- 如何在c#中把图片上传到服务器?
- 求助,这样的SQL语句如何实现
foreach (XmlNode xn in xmlnodeList)//开始
要遍历完成之后才可以决定是否添加。你的if else 这样的话始终会添加