Writing Xml from listView

I am trying to get each item from a listview and write it to an xml file. For instance lets say i have 2 items in the ListView: http://download.com/download1.zip & http://download.com/download2.zip. below is the code i have but it gives me an error: Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type "System.String'.

XmlNode nodeDload = xmlDoc.DocumentElement;
XmlElement Dload = xmlDoc.CreateElement("Download");
XmlElement dataDload = xmlDoc.CreateElement(i.ToString());
XmlText textDload = xmlDoc.CreateTextNode("");

nodeDload.AppendChild(Dload);

foreach (string item in aUrlView.Items)
{
Dload.AppendChild(dataDload);
dataDload.AppendChild(textDload);
textDload.Value = item;
++i;
}

Example Below

<root>
<download>
<1>http://download.com/download1.zip</1>
<2>http://download.com/download2.zip</2>
</download>
</root>

Could someone help me out with this
Thanks in advance


Answer this question

Writing Xml from listView

  • Mapa3matuk

    take a look at the following code and i hope this works.

    XmlElement DLoad = doc.CreateElement("DownLoad");

    for(i=0;i<lstView.Items.Count;i++)

    {

    XmlElement data = doc.CreateElement(i.ToString());

    XmlText textNode = doc.CreateTextNode("");

    textNode.InnerText = lstView.ItemsIdea.Text;

    data.AppendChild(textNode);

    DLoad.AppendChild(data);

    doc.DocumentElement.AppendChild(DLoad);

    }



  • logtorahul

    thanks you both very much for your help! I appreciate it
  • microslave

    The ListView.Items property returns a collection of ListViewItem objects not string objects. Try this:

    foreach (ListViewItem item in aUrlView.Items)
    {
    Dload.AppendChild(dataDload);
    dataDload.AppendChild(textDload);
    textDload.Value = item.Text;
    ++i;
    }

    Cheers,
    Kenny Kerr

    http://weblogs.asp.net/kennykerr/


  • Writing Xml from listView