<?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>
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; } }
<?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>
XmlAttribute Imgattribute1 = xmldoc.CreateAttribute("Id"); Imgattribute1.Value = "1";
XmlAttribute Imgatt1 = xmldoc.CreateAttribute("Id"); Imgatt1.Value = "2";
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)