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

时间:2012-03-06 23:20:18

标签: c# .net xml winforms c#-2.0

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

使用所需节点生成XML文件:

GenerateXML()

            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?

2 个答案:

答案 0 :(得分:1)

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

<强> GenerateXML()

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);

答案 1 :(得分:0)

尝试在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;