Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#将xml读入listview_C#_Xml_Readxml_Writexml - Fatal编程技术网

c#将xml读入listview

c#将xml读入listview,c#,xml,readxml,writexml,C#,Xml,Readxml,Writexml,编写XML代码 XmlTextWriter xmlchat = new XmlTextWriter("chatroomdoc.xml", UTF8Encoding.UTF8); xmlchat.WriteStartDocument(); xmlchat.Formatting = Formatting.Indented; xmlchat.WriteStartElement("Chat"); foreach (ListViewItem

编写XML代码

XmlTextWriter xmlchat = new XmlTextWriter("chatroomdoc.xml", UTF8Encoding.UTF8);
        xmlchat.WriteStartDocument();
        xmlchat.Formatting = Formatting.Indented;
        xmlchat.WriteStartElement("Chat");
        foreach (ListViewItem one in lvmess.Items)
        {
            xmlchat.WriteStartElement("Client", one.SubItems[0].Text);
            xmlchat.WriteElementString("Nickname", one.SubItems[0].Text);
            xmlchat.WriteElementString("Message",one.SubItems[1].Text);
            xmlchat.WriteEndElement();
        }
        xmlchat.WriteEndElement();

        xmlchat.WriteEndDocument();
        xmlchat.Close();
这段代码是我的客户端聊天应用程序的一部分,它将文本消息保存到xml文件中。我需要编写一个代码,在您打开客户端时将xml文件加载到listview。但是我无法使读取部分正确。lvmess.Items.Add会因为while而失败,或者当我在运行时在外部执行该操作时失败,但只有第一个项(显然)有人可以帮助我吗

这是读取文件代码

    private void Form1_Load(object sender, EventArgs e){

        ListViewItem lis = new ListViewItem();
        using (XmlReader reader = XmlReader.Create("chatroomdoc.xml"))
        {
            int i =0;

            while (reader.Read())
            {

                    switch (reader.Name.ToString())
                    {
                        case "Nickname":
                            lis.Text=reader.ReadElementContentAsString();
                        break;
                        case "Message":
                            lis.SubItems.Add(reader.ReadElementContentAsString());

                        break;
                    }
                //lvmess.Items.Add(lis);
                //i++;

            }

            lvmess.Items.Add(lis);
        }
    }

您已经在while循环之外创建了ListViewItem,并且在每次迭代中您都只是更改了同一ListViewItem上的数据。您应该在每次迭代中创建一个新项目,并将其添加到列表中,以便以后使用。大概是这样的:

private void Form1_Load(object sender, EventArgs e){

    //ListViewItem lis = new ListViewItem();
    using (XmlReader reader = XmlReader.Create("chatroomdoc.xml"))
    {
        int i =0;

        while (reader.Read())
        {
                ListViewItem lis = new ListViewItem();
                switch (reader.Name.ToString())
                {
                    case "Nickname":
                        lis.Text=reader.ReadElementContentAsString();
                    break;
                    case "Message":
                        lis.SubItems.Add(reader.ReadElementContentAsString());

                    break;
                }
                lvmess.Items.Add(lis);
            //i++;

        }

       // lvmess.Items.Add(lis);
    }
}

请为您的答案提供一些描述。