本文介绍了如何在新节点中自动增加id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//////通过这段代码,我输出为:
//////By this code i am getting output as:
<?xml version="1.0" encoding="UTF-8"?>
<Album>
<ImagesFolder>
<Image Id="1" Name="Image0356.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Image0356.jpg" />
<Image Id="2" Name="4.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\4.jpg" />
<Image Id="2" Name="Shilpa1.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Shilpa1.jpg" />
</ImagesFolder>
<VideosFolder>
<Video />
</VideosFolder>
</Album>
///////这是我的代码////
/////// this is my code////
public void createxml()
{
XmlDocument xmldoc = new XmlDocument();
XmlNode ImagesFolder = xmldoc.CreateElement("ImagesFolder");
XmlNode VideosFolder = xmldoc.CreateElement("VideosFolder");
XmlNode Image = xmldoc.CreateElement("Image");
XmlNode Video = xmldoc.CreateElement("Video");
XmlNode albumnode = xmldoc.CreateElement("Album");
try
{
if (xmlFolder.Exists)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("") + "\\" + txtalbum.Text + "\\" + "XML\\");
FileInfo[] TXTFiles = di.GetFiles("*.xml");
if (TXTFiles.Length == 0)
{
XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode);
XNamespace adlcp = "http://www.w3.org/2001/XMLSchema-instance";
xmldoc.AppendChild(albumnode);
//XmlNode ImagesFolder = xmldoc.CreateElement("ImagesFolder");
//XmlNode VideosFolder = xmldoc.CreateElement("VideosFolder");
//XmlNode Image = xmldoc.CreateElement("Image");
//XmlNode Video = xmldoc.CreateElement("Video");
XmlAttribute Imgattribute1 = xmldoc.CreateAttribute("Id");
Imgattribute1.Value = "1";
Image.Attributes.Append(Imgattribute1);
XmlAttribute Imgattribute2 = xmldoc.CreateAttribute("Name");
Imgattribute2.Value = FileUpload1.FileName;
Image.Attributes.Append(Imgattribute2);
XmlAttribute Imgattribute3 = xmldoc.CreateAttribute("ImagePath");
Imgattribute3.Value = "" + location;
Image.Attributes.Append(Imgattribute3);
albumnode.AppendChild(ImagesFolder);
albumnode.AppendChild(VideosFolder);
ImagesFolder.AppendChild(Image);
VideosFolder.AppendChild(Video);
xmldoc.Save(Server.MapPath("" + "~\\" + txtalbum.Text + "\\XML\\" + txtalbum.Text + ".xml"));
}
else
{
//updating the nodes
xmldoc.Load(Server.MapPath("" + "~\\" + txtalbum.Text + "\\XML\\" + txtalbum.Text + ".xml"));
int flgnode = 0;
XmlNodeList nodelist = xmldoc.GetElementsByTagName("Image");
for (int i = 0; i < nodelist.Count; i++)
{
string getnode = nodelist[i].Attributes["Name"].Value;
if (FileUpload1.FileName.Equals(getnode))
{
flgnode = 1;
Response.Write(FileUpload1.FileName + " Is already Present. Please Choose Another");
}
}
if (flgnode == 0)
{
XmlNode newimg = xmldoc.CreateElement("Image");
XmlAttribute Imgatt1 = xmldoc.CreateAttribute("Id");
Imgatt1.Value = "2";
newimg.Attributes.Append(Imgatt1);
XmlAttribute Imgatt2 = xmldoc.CreateAttribute("Name");
Imgatt2.Value = FileUpload1.FileName;
newimg.Attributes.Append(Imgatt2);
XmlAttribute Imgatt3 = xmldoc.CreateAttribute("ImagePath");
Imgatt3.Value = "" + location;
newimg.Attributes.Append(Imgatt3);
xmldoc.DocumentElement.AppendChild(newimg);
XmlNodeList lstxmlNode = xmldoc.GetElementsByTagName("ImagesFolder");
lstxmlNode[0].AppendChild(newimg);
xmldoc.Save(Server.MapPath("~/"+txtalbum.Text+"/XML/Traditional Day 2011.xml"));
Response.Write("Image Add Successfully...");
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
///我希望输出为/////
/// i want output as/////
<?xml version="1.0" encoding="UTF-8"?>
<Album>
<ImagesFolder>
<Image Id="1" Name="Image0356.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Image0356.jpg" />
<Image Id="2" Name="4.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\4.jpg" />
<Image Id="3" Name="Shilpa1.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Shilpa1.jpg" />
</ImagesFolder>
<VideosFolder>
<Video />
</VideosFolder>
</Album>
//我该怎么做..
// how can i do this..
推荐答案
XmlAttribute Imgattribute1 = xmldoc.CreateAttribute("Id");
Imgattribute1.Value = "1";
XmlAttribute Imgatt1 = xmldoc.CreateAttribute("Id");
Imgatt1.Value = "2";
因此,请更改您的代码。
So, change your code.
这篇关于如何在新节点中自动增加id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!