Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//////By this code i am getting output as:
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="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////
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

                  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
<?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..
Posted
Updated 21-Jan-14 21:24pm
v4
Comments
kedar001 22-Jan-14 2:01am    
What have you tried ?

1 solution

Do you realize that you have hard coded the ids. See below code.
C#
XmlAttribute Imgattribute1 = xmldoc.CreateAttribute("Id");
Imgattribute1.Value = "1";

C#
XmlAttribute Imgatt1 = xmldoc.CreateAttribute("Id");
Imgatt1.Value = "2";


So, change your code.
 
Share this answer
 

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