Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
by using this code i am getting output as:
output:

XML
<?xml version="1.0" encoding="UTF-8"?>
<Album>
  <ImagesFolder>
    <Image Id="1" Name="Image0356.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Image0356.jpg" />
  </ImagesFolder>
  <VideosFolder>
    <Video />
  </VideosFolder>
  <Image Id="2" Name="Pushkraj 214187.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Pushkraj 214187.jpg" />
  <Image Id="2" Name="7.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\7.jpg" />
  <Image Id="2" Name="DSC01049.JPG" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\DSC01049.JPG" />
</Album>



////////////my code////


C#
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

                   //my code starts///////////
               
                    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);

                        albumnode.AppendChild(ImagesFolder); 
                        ImagesFolder.AppendChild(newimg);
                        xmldoc.DocumentElement.AppendChild(newimg);        
                        
            

                     
                        xmldoc.Save(Server.MapPath("~/"+txtalbum.Text+"/XML/Traditional Day 2011.xml"));
                        Response.Write("Image Add Successfully...");                       
                      
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }



//but the output should be
XML
<?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="Pushkraj 214187.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\Pushkraj 214187.jpg" />
    <Image Id="3" Name="7.jpg" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\7.jpg" />
    <Image Id="4" Name="DSC01049.JPG" ImagePath="D:\OrgWebsite\Traditional Day 2011\Images\DSC01049.JPG" />
  </ImagesFolder>
  <VideosFolder>
    <Video />
  </VideosFolder>

</Album>



///Where i am going wrong???
Posted
Updated 21-Jan-14 19:15pm
v2

1 solution

try this Code

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);
 
 XmlNodeList lstxmlNode=xmldoc.GetElementsByTagName("ImagesFolder")
lstxmlNode[0].AppendChild(newimg);
 
Share this answer
 
v2
Comments
sumit kausalye 22-Jan-14 1:42am    
it worked, thank u so much..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900