在c#中创建运行时XML并保存数据

本文关键字:保存 数据 XML 运行时 创建 | 更新日期: 2023-09-27 18:27:02

试图创建动态XML文件并将数据保存到其中,但我能够保存最后一个条目,而不是所有条目。有什么想法吗?

生成具有所需节点的XML文件:

生成XML()

            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);
            XmlNode albums = doc.CreateElement("albums");
            doc.AppendChild(albums);
            XmlNode album = doc.CreateElement("album");
            albums.AppendChild(album);
            XmlNode title = doc.CreateElement("title");
            albums.AppendChild(title);
            doc.Save(System.Windows.Forms.Application.StartupPath + "''albums.xml");

点击按钮的输出给我10条记录或100条记录或一些数量的记录:

 //album is a class returned by web service
if (Elements.Count > 0)
                {
             string path = System.Windows.Forms.Application.StartupPath + "''albums.xml";
                //Read the file stream in read mode
                FileStream READER = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Xml.XmlDocument albumSpecs = new System.Xml.XmlDocument();
                albumSpecs.Load(READER);
                FileStream WRITER = null; 
                GenerateXML();
                //Create a FileStream for writing
                WRITER = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);

                XmlElement root = doc.DocumentElement;
                XmlNodeList nodes = root.SelectNodes("//albums");
                for (int i = 0; i < ResultsList.Items.Count - 1; i++)
                {
                    album = (Album)ResultsList.Items[i]; 
                    foreach (XmlNode node in nodes)
                    {
                        node["title"].InnerText = album.Title;
                    }
                }
                //Write the data to the filestream
                doc.Save(WRITER);
            }

我得到XML中的最后一个条目数据,而不是所有返回的条目。

<?xml version="1.0" encoding="UTF-8"?>
<albums>
  <album />
  <title>She will be loved</title>
</albums>

如何根据返回的项数构建运行时XML?

在c#中创建运行时XML并保存数据

首先将已保存的xml加载到XmlDocument中,然后向其中添加新元素。。。

生成XML()

string xmlFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "albums.xml");
        XmlDocument doc = new XmlDocument();
        XmlNode albums = null;
        if (!File.Exists(xmlFilePath))
        {
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);
            albums = doc.CreateElement("albums");
            doc.AppendChild(albums);
        }
        else
        {
            doc.Load(xmlFilePath);
            albums = doc.ChildNodes[1];
        }
        XmlNode album = doc.CreateElement("album");
        albums.AppendChild(album);
        XmlNode albumName = doc.CreateElement("name");
        album.AppendChild(albumName);

        XmlNode title = doc.CreateElement("title");
        album.AppendChild(title);
        doc.Save(xmlFilePath);

是否尝试向XML文档添加元素?

for (int i = 0; i < ResultsList.Items.Count - 1; i++)
            {
                album = (Album)ResultsList.Items[i]; 
                foreach (XmlNode node in nodes)
                {
                    node["title"].InnerText = album.Title;
                }
            }

这个代码示例的作用不大,因为您只有一个元素可以迭代。。。

编辑:我想你想做这样的事情:

for(int i = 0; i < ResultsList.Items.count -1; i++)
    {
         XmlNode child = doc.createElement("Title");
         doc.AppendChild .....
    }
doc.save;