如何在现有节点中添加新节点? [英] How to add new node in existing node?

查看:174
本文介绍了如何在现有节点中添加新节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码输出为:



with my code i am getting output as:

<?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 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?

推荐答案

请做没有重新发布同样的问题;您已经在如何添加新节点的答案中得到答案现有节点? [ ^ ]。
Please do not repost the same question; you have already been given an answer at How to add new node in existing node?[^].


这篇关于如何在现有节点中添加新节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆