2017-09-06 5 views
0

xmlドキュメントのすべてのチェックデータを最後に表示できません が選択されましたチェックリストボックスアイテムがxmlに表示されました xmlのすべてのチェックデータを表示してください私はこれを行うことで..すべてのcheckledistboxアイテムをWindowsフォームアプリケーションのXmlドキュメントにエクスポートする方法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.. 

答えて

0

に行くあなたの質問は、正しいコードが含まれている、唯一の代わりにcheckedListBox1.SelectedItems

のコード checkedListBox1.CheckedItemsを使用することである必要とする変更:

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

これは非常に簡単になりました。 checkedListBox1.Item [i]は文字列値であり、明示的変換により変数にロードすることができます。次のコードが動作します:

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); 
} 

これでアイテムが見つかりました。 XMLファイルに書き込むことができます。詳細については、XMLのDOC

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

への書き込み

Official Site

+0

要素がメッセージボックスにアップpopingたが、最後に選択されていますアイテムがxmlにエクスポートされています – Mounika

+0

[Mounika](https://stackoverflow.com/users/8489669/mounika)、私は私の答えを更新しました。今すぐチェックしてください。 –

+0

チェック項目はXML文書には表示されず、最後に選択した項目項目のみが表示されます。チェックした項目をすべてXML文書に書いてください。 – Mounika

関連する問題