从xml c#加载列表并输出到列表框 [英] load list from xml c# and output to listbox

查看:116
本文介绍了从xml c#加载列表并输出到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,我想将项目保存到xml文件中.
这是要保存的代码:

i have a listbox, and i want to save the items into an xml file.
this is the code to save:

try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(string));
                TextWriter tw = new StreamWriter("C:\\Users\\Lifebook\\Desktop\\Usage Profiles.txt");
                for (int i = 0; i <= lstUsageProfile.Items.Count - 1; i++)
                {
                    serializer.Serialize(tw, lstUsageProfile.Items[i].ToString());
                }
                tw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving");
            }


这是xml
的输出


This is the output of the xml

<string><node1></string>




对于列表框中的每个节点重复此行.

保存它们没问题,但是然后我想加载它们(供正在使用该程序的下一个用户使用)

但我无法加载它们并将它们放回列表框中,并删除默认情况下创建的xml标签.
请帮助.



在该链接之后,我将代码改进为:




this line is repeated for every node in the list box.

to save them it''s no problem, but then i want to load them (for the next user who is working on the program)

but i can''t load them up and put them back in the list box and removing the xml tags created by default.
pls help.



after that link, i improved my code to:

XmlSerializer deserializer = new XmlSerializer(typeof(List<string>));
TextReader textReader = new StreamReader("C:\\Users\\Lifebook\\Desktop\\Usage Profiles.txt");
List<string> up;
up = (List<string>)deserializer.Deserialize(textReader);
textReader.Close();

lstUsageProfile.Items.Add(up);</string></string></string>




我收到此错误

XML文档(2,2)中存在错误.




and i''m getting this error

There is an error in XML document (2, 2).

推荐答案

列表是朝正确方向的更改.看看以下用于导出和导入的代码:

The List was a change in the right direction. Take a look at this code for export and Import:

private void ExportXML()
{
   try
   {
      System.Collections.Generic.List<string> theList = new System.Collections.Generic.List<string>();
      XmlSerializer serializer = new XmlSerializer(typeof(System.Collections.Generic.List<string>));
      TextWriter tw = new StreamWriter(@"C:\Users\Lifebook\Desktop\Usage Profiles.txt");

      for (int i = 0; i <= lstUsageProfile.Items.Count - 1; i++)
      {
         theList.Add(lstUsageProfile.Items[i].ToString());
      }

      serializer.Serialize(tw, theList);
      tw.Close();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message.ToString());
   }
}


private void ReadXML()
{
   try
   {
      System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
      doc.Load(@"C:\Users\Lifebook\Desktop\Usage Profiles.txt");
      System.Xml.XmlElement root = doc.DocumentElement;
      System.Xml.XmlNodeList lst = root.GetElementsByTagName("string");

      foreach (System.Xml.XmlNode n in lst)
      {
         lstUsageProfile.Items.Add(n.InnerText);
      }
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message.ToString());
   }
}


您应该看一下这篇文章.
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization [ ^ ]
You should take a look to this article.
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization[^]


这篇关于从xml c#加载列表并输出到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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