Click here to Skip to main content
15,921,179 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
with my code i am getting output as:

XML
<?xml version="1.0" encoding="UTF-8"?>
<AlbumFolder>
  <Album Name="A">
    <ImagesFolder>D:\OrgWebsite\A\Images</ImagesFolder>
    <Album>
      <VideosFolder>D:\OrgWebsite\A\Videos</VideosFolder>
    </Album>
  </Album>
 </AlbumFolder>


but i want output in this format..

XML
<?xml version="1.0" encoding="UTF-8"?>
<AlbumFolder>
  <Album Name="A">
    <ImagesFolder>D:\OrgWebsite\A\Images</ImagesFolder>
   <VideosFolder>D:\OrgWebsite\A\Videos</VideosFolder>
  </Album>
 </AlbumFolder>




this the code i am using...

public void vdoxml()
    {
        string subPath = txtalbum.Text;

        XmlDocument xmldoc = new XmlDocument();
        XmlNode VideosFolder = xmldoc.CreateElement("VideosFolder");
        XmlNode albumnode = xmldoc.CreateElement("AlbumFolder");
        XmlNode album = xmldoc.CreateElement("Album");

        string path = Server.MapPath("") + "\\" + txtalbum.Text + "\\" + "Videos";


        string imglocation = Server.MapPath("~/") + "XML/";

        bool isExists = System.IO.Directory.Exists(Server.MapPath(subPath));
        if (!isExists)
        {

            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/") + "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);

                XmlAttribute albumattribute1 = xmldoc.CreateAttribute("Name");
                albumattribute1.Value = txtalbum.Text;
                album.Attributes.Append(albumattribute1);

                VideosFolder.InnerText = path;
                albumnode.AppendChild(album);
                album.AppendChild(VideosFolder);
                xmldoc.Save(Server.MapPath("" + "~/" + "XML/album.xml"));

            }
            else
            {
                xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));
                int flgnode = 0;

                if (flgnode == 0)
                {


                    XmlAttribute albumattribute1 = xmldoc.CreateAttribute("Name");
                    albumattribute1.Value = txtalbum.Text;
                    album.Attributes.Append(albumattribute1);

                    VideosFolder.InnerText = path;
                    albumnode.AppendChild(album);
                    album.AppendChild(VideosFolder);

                    XmlNodeList lstxmlNode = xmldoc.GetElementsByTagName("AlbumFolder");
                    lstxmlNode[0].AppendChild(album);
                    xmldoc.Save(Server.MapPath("~/" + "XML/album.xml"));

                }
            }
        }//if album folder already exists.
        else
        {
            xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));
            int flgsubnode = 0;
            XmlNodeList nodelist = xmldoc.GetElementsByTagName("Album");
            for (int i = 0; i < nodelist.Count; i++)
            {
                string anode = nodelist[i].Attributes["Name"].Value;
                if (txtalbum.Text.Equals(anode))
                {
                    flgsubnode = 1;
                    VideosFolder.InnerText = path;
                    albumnode.AppendChild(album);
                    album.AppendChild(VideosFolder);

                    nodelist = xmldoc.GetElementsByTagName("Album");
                    nodelist[i].AppendChild(album);
                    xmldoc.Save(Server.MapPath("~/" + "XML/album.xml"));

                   // Response.Write(txtalbum.Text + " Is already Present. Please Choose Another");
                }
            }
            if (flgsubnode == 0)
            {
                VideosFolder.InnerText = path;
                albumnode.AppendChild(album);
                album.AppendChild(VideosFolder);

                XmlNodeList lstxmlNode = xmldoc.GetElementsByTagName("Album");
                lstxmlNode[0].AppendChild(album);
                xmldoc.Save(Server.MapPath("~/" + "XML/album.xml"));
               
            }

            //xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));

            //VideosFolder.InnerText = path;
            //albumnode.AppendChild(album);
            //album.AppendChild(VideosFolder);

            //XmlNodeList lstxmlNode = xmldoc.GetElementsByTagName("Album");
            //lstxmlNode[0].AppendChild(album);
            //xmldoc.Save(Server.MapPath("~/" + "XML/album.xml"));
        }
    }

where i am going wrong?
Posted
Updated 23-Jan-14 20:29pm
v3

1 solution

Please do not repost the same question; you have already been given an answer at How to add new node in existing node?[^].
 
Share this answer
 
Comments
sumit kausalye 24-Jan-14 7:01am    
question same but problem is different.

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