How to write XML file from a ListView in C#

2019-06-01 04:01发布

i'm having a ListView control like

alt text

I need to store the data from this ListView to an Xml File.

So the xml file will be like

<root>
 <Child Name ="A1" val1="1" val2="0"/>
 <Child Name ="A2" val1="1" val2="2"/>
 <Child Name ="A3" val1="1" val2="3"/>
 <Child Name ="A4" val1="1" val2="4"/>
 <Child Name ="A5" val1="1" val2="5"/>
 <Child Name ="A6" val1="6" val2="0"/>
 <Child Name ="A7" val1="7" val2="0"/>
</root>

if the data is stored in some List or Dictionary , then i know to do this using XML to LINQ

But how do i do this from a ListView

XDocument XD=new XDocument(new XElement("root",........// what i have to do here.......

Please help me to do this...

Thanks in advance

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-01 04:34

You can query the list view subitems along with its column headers:

XDocument document = new XDocument(new XElement("root",
    from item in yourListView.Items.Cast<ListViewItem>()
    select new XElement("Child",
        item.SubItems.Cast<ListViewSubItem>()
            .Select((subitem, i) => new XAttribute(
                i == 0 ? "Name" : yourListView.Columns[i].Text.ToLower(),
                subItem.Text)))));

EDIT: Since neither ListViewItemCollection nor ListViewSubItemCollection support query operators out of the box, we need to call Cast<T>() to be able to use them.

查看更多
smile是对你的礼貌
3楼-- · 2019-06-01 04:41

Here is a simple example. The goal here is to do it using XmlTextWriter. Of course, this sample needs to be modified to suit your particular file I/O and XML-formatting needs:

public static bool ExportListViewlToXML(ListView listview, String filePath, String fileName)
    {
        FileStream fileStream;
        StreamWriter streamWriter;
        XmlTextWriter xmlTextWriter;

        try
        {
            // overwrite even if it already exists
            fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);

            streamWriter = new StreamWriter(fileStream);
            xmlTextWriter = new XmlTextWriter(streamWriter);
            xmlTextWriter.Formatting = Formatting.Indented;
            xmlTextWriter.WriteStartDocument();
            xmlTextWriter.WriteStartElement("Items");

            const int SUBITEM1_POS = 0;
            const int SUBITEM2_POS = 1;
            const int SUBITEM3_POS = 2;

            for (int i = 0; i < listview.Items.Count; i++)
            {
                String currentSubItem1 = listview.Items[i].SubItems[SUBITEM1_POS].Text;
                String currentSubItem2 = listview.Items[i].SubItems[SUBITEM2_POS].Text;
                String currentSubItem3 = listview.Items[i].SubItems[SUBITEM3_POS].Text;

                xmlTextWriter.WriteStartElement("Item");
                xmlTextWriter.WriteAttributeString("subitem1", currentSubItem1.ToString());
                xmlTextWriter.WriteAttributeString("subitem2", currentSubItem2.ToString());
                xmlTextWriter.WriteAttributeString("subitem3", currentSubItem3.ToString());
                xmlTextWriter.WriteEndElement();
            }

            xmlTextWriter.WriteEndDocument();
            xmlTextWriter.Flush();
            xmlTextWriter.Close();

            return true;
        }
        catch (IOException ex)
        {
            // do something about your error
            return false;
        }
    }
查看更多
登录 后发表回答