2017-09-06 4 views
0

Je ne suis pas en mesure d'afficher toutes les données vérifiées dans le document XML seulement dernier sélectionné Checkedlistbox élément est affiché en xml je voulais afficher toutes les données vérifiées en xml s'il vous plaît aide moi en faisant cela ..comment exporter tous les éléments checkledistbox au document Xml sous Windows formulaire application C#

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.IO; 
    using System.Xml; 
    namespace GetDetails 
    { 
      public partial class Form1 : Form 
     { 

     public Form1() 
     { 
     InitializeComponent(); 

     } 


    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void button4_Click(object sender, EventArgs e) 
    { 
     // Create a new instance of FolderBrowserDialog. 
     FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog(); 
     // A new folder button will display in FolderBrowserDialog. 
     folderBrowserDlg.ShowNewFolderButton = true; 
     //Show FolderBrowserDialog 
     DialogResult dlgResult = folderBrowserDlg.ShowDialog(); 
     if (dlgResult.Equals(DialogResult.OK)) 
     { 
      //Show selected folder path in textbox1. 
      textBox1.Text = folderBrowserDlg.SelectedPath; 
      //Browsing start from root folder. 
      Environment.SpecialFolder rootFolder = 
       folderBrowserDlg.RootFolder; 
     } 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     FolderBrowserDialog fbd = new FolderBrowserDialog(); 
     if (fbd.ShowDialog() == DialogResult.OK) 
     { 
      if (!textBox1.Text.Equals(String.Empty)) 
      { 
       if (System.IO.Directory.GetFiles(textBox1.Text).Length > 0) 
       { 
        foreach (string file in 
       System.IO.Directory.GetFiles(textBox1.Text)) 
        { 
         //Add file in ListBox. 
         checkedListBox1.Items.Add(Path.GetFileName(file)); 
        } 
       } 
       else 
       { 
        checkedListBox1.Items.Add(String.Format("No files Found 
       at location: { 0}", textBox1.Text)); 
       } 
      } 
      /* 
      FolderBrowserDialog fbd = new FolderBrowserDialog(); 
      if(fbd.ShowDialog() == DialogResult.OK) 
      { 
       checkedListBox1.Items.Clear(); 
       string[] files = Directory.GetFiles(fbd.SelectedPath); 
       string[] dirs = Directory.GetDirectories(fbd.SelectedPath); 

       foreach(string file in files) 
       { 
        checkedListBox1.Items.Add(Path.GetFileName(file)); 
       } 
       foreach (string dir in dirs) 
       { 
        checkedListBox1.Items.Add(Path.GetFileName(dir)); 
       } 
      }*/ 

      } 
     } 

    private void checkedListBox1_SelectedIndexChanged(object sender, 
    EventArgs e) 
    { 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     XmlTextWriter xwriter = new XmlTextWriter("GetDetails.xml", 
     Encoding.Unicode); 
     xwriter.WriteStartDocument(); 
     xwriter.WriteStartElement("XMLFILE"); 
     xwriter.WriteStartElement("file"); 
     xwriter.WriteString(textBox1.Text); 
     xwriter.WriteEndElement(); 

      foreach (String item in checkedListBox1.SelectedItems) 
      { 
       xwriter.WriteStartElement("SelectedItems"); 
       xwriter.WriteString(item); 
       xwriter.WriteEndElement(); 

       /* for (int i = 0; i < 
      checkedListBox1.CheckedIndices.Count; i++) 
        { 
         //checkedListBox1.ClearSelected(); 

     checkedListBox1.Items.Add(checkedListBox1.CheckedIndices[i]); 
        }*/ 
      } 
      xwriter.WriteEndElement(); 
      xwriter.WriteEndDocument(); 
      xwriter.Close(); 

     } 


    private void button3_Click(object sender, EventArgs e) 
    { 
     this.Close(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void button5_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
} 


} 
    only last selected Checkedlistbox item is displayed in xml i wanted to 
display all checked data in xml please help me in doing this.. 

Répondre

0

Votre question contient du code correct, modifier uniquement ce qui est nécessaire est d'utiliser checkedListBox1.CheckedItems au lieu de checkedListBox1.SelectedItems

code:

private void button2_Click(object sender, EventArgs e) 
{ 
    XmlTextWriter xwriter = new XmlTextWriter("GetDetails.xml", Encoding.Unicode); 
    xwriter.WriteStartDocument(); 
    xwriter.WriteStartElement("XMLFILE"); 
    xwriter.WriteStartElement("file"); 
    xwriter.WriteString(textBox1.Text); 
    xwriter.WriteEndElement(); 

    foreach (var item in checkedListBox1.CheckedItems) 
    { 
     xwriter.WriteStartElement("SelectedItems"); 
     xwriter.WriteString(item.ToString()); 
     xwriter.WriteEndElement(); 

    } 
    xwriter.WriteEndElement(); 
    xwriter.WriteEndDocument(); 
    xwriter.Close(); 
} 
0

Cela a fini par être assez simple. checkedListBox1.Item [i] est une valeur de chaîne, et une conversion explicite a permis de la charger dans une variable. Le code suivant fonctionne:

private void button2_Click(object sender, EventArgs e) 
{ 
    string str=""; 
    for (int i = 0; i < checkedListBox1.Items.Count; i++) 
    { 
      if (checkedListBox1.GetItemChecked(i)) 
     { 
      str += (string)checkedListBox1.Items[i]; 

     } 
    } 
    MessageBox.Show(str); 
} 

Maintenant vous avez les articles. Vous pouvez écrire dans un fichier XML.

pour l'écriture en XML DOC

System.IO.File.WriteAllText("GetDetails.xml", str); 

Pour plus d'informations. Aller à Official Site

+0

éléments sont POPING dans la boîte de message, mais, seul dernier sélectionné article est l'exportation au xml – Mounika

+0

[Mounika] (https://stackoverflow.com/users/8489669/mounika), j'ai mis à jour ma réponse. Vérifie-le maintenant. –

+0

les éléments cochés ne sont pas affichés dans le document xml, seul le dernier article sélectionné s'affiche, je vous prie de me suggérer par écrit tous les éléments cochés au document xml. – Mounika