从xml读取时,Combobox只更新一个值 [英] Combobox is updated with only one value while reading it from xml

查看:43
本文介绍了从xml读取时,Combobox只更新一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以告诉我,当我从xml填充数据到组合框时,我将丢失所有以前的数据&仅显示有界数据。我想从xml&读取数据在组合框中显示,比如显示保存的数据。



我的代码如下所示:



< pre lang =c#> string dsFileName = tst1 .XML;
DataSet dsFeeds = new DataSet();
dsFeeds.ReadXml(dsFileName);
stg1vtgtypcmbbox.DataSource = dsFeeds.Tables [ 0 ];
stg1vtgtypcmbbox.DisplayMember = VOLTAGETYPE;
stg1vtgtypcmbbox.ValueMember = VOLTAGETYPE;





我的xml文件包含如下所示的数据:



 <   tst2  >  
< STAGE1 >
< VOLTAGETYPE >
40
< / VOLTAGETYPE >
< OP_VAL >
101
< / OP_VAL >
< DEF_INV >
YES
< / DEF_INV >
< OP_TIME >
0.00
< / OP_TIME >
< RST_VAL >
85
< / RST_VAL >
< RST_TIME >
0.00
< / RST_TIME >
< / STAGE1 >
< / tst2 >





但是在加载事件期间,stg1vtgcmbbox包含20,40,65等值。完成上述操作后,组合框被清除&只显示40。这不应该发生,我希望以前的值也存在&显示从xml文件中读取的值。

解决方案

以下是我正在使用此解决方案的一些示例XML。



 <?  xml     version   =  1.0   编码  =  utf-8  >  
< 数据 >
< comboBox id = comboBox1 >
< item displayName = ComboBox1 Test 1 value = ComboBox1测试1 已选择 = false / >
< item displayName = ComboBox1测试2 = ComboBox1测试2 已选择 = true / >
< item displayName = ComboBox1 Test 3 value = ComboBox1测试3 selec ted = false / >
< item displayName = ComboBox1 Test 4 value = ComboBox1 Test 4 已选择 = false / >
< item displayName = ComboBox1测试5 value = ComboBox1测试5 已选择 < span class =code-keyword> = false / >
< / comboBox >
< comboBox id = comboBox2 >
< item displayName = ComboBox2 Test 1 value = ComboBox1测试1 已选择 = false / >
< item displayName = ComboBox2测试2 value = ComboBox1 Test 2 已选择 = false / >
< item displayName = ComboBox2 Test 3 value = < span class =code-keyword> ComboBox1 Test 3 selected = true / >
< item displayName = ComboBox2测试4 value = ComboBox1 Test 4 < span class =code-attribute> selected = false / > ;
< item displayName = ComboBox2 Test 5 value = ComboBox1测试5 selected <> span = false / >
< / comboBox >
< comboBox id = comboBox3 >
< item displayName = ComboBox3测试1 = ComboBox1 Test 1 selected = false < span class =code-keyword> / >
< item displayName = ComboBox3测试2 = C omboBox1测试2 已选择 = false / >
< item < span class =code-attribute> displayName = ComboBox3 Test 3 value = ComboBox1测试3 已选中 = false / >
< item displayName = ComboBox3 Test 4 value = ComboBox1 Test 4 已选择 = true / >
< item displayName = ComboBox3测试5 value = ComboBox1 Test 5 已选择 = false / >
< / comboBox >
< / data >





所以第一步是从XML文件加载数据并将其绑定到表单上的ComboBox对象。 br $>


 私人  void  loadButton_Click( object  sender,EventArgs e)
{
var comboBoxData = XDocument.Load( ComboBoxData.xml) ;

foreach var 元素 in comboBoxData.Element( data)。元素( comboBox))
{
ComboBox comboBox =
(ComboBox)(Controls.Find (element.Attribute( id)。值,))首先();

comboBox.DataSource =
来自数据 元素。元素( item
select new ComboBoxItem
{
ItemDisplayName = data.Attribute( < span class =code-string> displayName
)。值,
ItemValue = data.Attribute( value)。值
})。ToList();

comboBox.DisplayMember = ItemDisplayName;
comboBox.ValueMember = ItemValue;

comboBox.SelectedValue =
来自数据 元素。元素( item
其中 data.Attribute( selected)。值== true
选择 data.Attribute( value)。Value).First();
}
}







我使用XDocument加载来自的数据文件。如果文件太大,这将不是处理此数据的有效方法。下一步是遍历表单上的所有ComboBox对象。使用存储在XML文件中的控件的ID,我能够在表单上找到控件并直接使用它。我使用LINQ投影来构建可以绑定到ComboBox的对象列表。我设置了显示名称和值成员,我还选择了要显示selected属性为true的项目。



现在所有的ComboBox对象绑定到XML文件,下一个有趣的部分是保存他们的状态。



  private   void  saveButton_Click( object  sender,EventArgs e)
{
var comboBoxData = new XDocument();
var dataRootElement = new XElement( data);

foreach (控制控件 this .Controls)
{
if (control ComboBox)
{
var comboBoxElement = new XElement( comboBox);
comboBoxElement.SetAttributeValue( id,control.Name);

for int index = 0 ; index < ((ComboBox)控件).Items.Count; index ++)
{
ComboBoxItem item =
(ComboBoxItem)((ComboBox)控件).Items [index];

var itemElement = new XElement( item);
itemElement.SetAttributeValue( displayName,item.ItemDisplayName);
itemElement.SetAttributeValue( value,item.ItemValue);
itemElement.SetAttributeValue( selected
((ComboBox)控件) .SelectedIndex == index? true false);

comboBoxElement.Add(itemElement);
}

dataRootElement.Add(comboBoxElement);
}
}

comboBoxData.Add(dataRootElement);
comboBoxData.Save( ComboBoxData.xml);
}





实际上这听起来更容易。我使用XDocument和XElement对象来构造文档的每个XML元素。我使用属性来存储对象的状态。



希望这给你一个良好的开端!


Hi all,

Can anybody tell me when i populate data to combobox from xml i will be loosing all the previous data & displays only the bounded data. I want to read data from xml & display in combobox, something like display saved data.

My code is as shown below:

string dsFileName = "tst1.xml";
DataSet dsFeeds = new DataSet();
dsFeeds.ReadXml(dsFileName);
stg1vtgtypcmbbox.DataSource = dsFeeds.Tables[0];
stg1vtgtypcmbbox.DisplayMember = "VOLTAGETYPE";
stg1vtgtypcmbbox.ValueMember = "VOLTAGETYPE";



My xml file contains data like shown below:

<tst2>
<STAGE1>
<VOLTAGETYPE>
40
</VOLTAGETYPE>
<OP_VAL>
101
</OP_VAL>
<DEF_INV>
YES
</DEF_INV>
<OP_TIME>
0.00
</OP_TIME>
<RST_VAL>
85
</RST_VAL>
<RST_TIME>
0.00
</RST_TIME>
</STAGE1>
</tst2>



But during load event the stg1vtgcmbbox contains values like 20, 40, 65.. After doing the above operation the combobox is cleared & only 40 is displayed. This shouldn't happen, i want the previous values also to be present & display the value that is read from xml file.

解决方案

Here is some sample XML I'm using for this solution.

<?xml version="1.0" encoding="utf-8"?>
<data>
  <comboBox id="comboBox1">
    <item displayName="ComboBox1 Test 1" value="ComboBox1 Test 1" selected="false" />
    <item displayName="ComboBox1 Test 2" value="ComboBox1 Test 2" selected="true" />
    <item displayName="ComboBox1 Test 3" value="ComboBox1 Test 3" selected="false" />
    <item displayName="ComboBox1 Test 4" value="ComboBox1 Test 4" selected="false" />
    <item displayName="ComboBox1 Test 5" value="ComboBox1 Test 5" selected="false" />
  </comboBox>
  <comboBox id="comboBox2">
    <item displayName="ComboBox2 Test 1" value="ComboBox1 Test 1" selected="false" />
    <item displayName="ComboBox2 Test 2" value="ComboBox1 Test 2" selected="false" />
    <item displayName="ComboBox2 Test 3" value="ComboBox1 Test 3" selected="true" />
    <item displayName="ComboBox2 Test 4" value="ComboBox1 Test 4" selected="false" />
    <item displayName="ComboBox2 Test 5" value="ComboBox1 Test 5" selected="false" />
  </comboBox>
  <comboBox id="comboBox3">
    <item displayName="ComboBox3 Test 1" value="ComboBox1 Test 1" selected="false" />
    <item displayName="ComboBox3 Test 2" value="ComboBox1 Test 2" selected="false" />
    <item displayName="ComboBox3 Test 3" value="ComboBox1 Test 3" selected="false" />
    <item displayName="ComboBox3 Test 4" value="ComboBox1 Test 4" selected="true" />
    <item displayName="ComboBox3 Test 5" value="ComboBox1 Test 5" selected="false" />
  </comboBox>
</data>



So the first step is to load the data from the XML file and bind it to the ComboBox objects on the form.

private void loadButton_Click(object sender, EventArgs e)
{
    var comboBoxData = XDocument.Load("ComboBoxData.xml");

    foreach (var element in comboBoxData.Element("data").Elements("comboBox"))
    {
        ComboBox comboBox =
            (ComboBox)(Controls.Find(element.Attribute("id").Value, false)).First();

        comboBox.DataSource =
            (from data in element.Elements("item")
             select new ComboBoxItem
             {
                 ItemDisplayName = data.Attribute("displayName").Value,
                 ItemValue = data.Attribute("value").Value
             }).ToList();

        comboBox.DisplayMember = "ItemDisplayName";
        comboBox.ValueMember = "ItemValue";

        comboBox.SelectedValue =
            (from data in element.Elements("item")
             where data.Attribute("selected").Value == "true"
             select data.Attribute("value").Value).First();
    }
}




I use an XDocument to load the data from the file. If your files get too large, this won't be an efficient way to process this data. Next step is to loop through all of the ComboBox objects on the form. Using the ID of the control stored in the XML file, I'm able to find the control on the form and work with it directly. I use LINQ projection to build object list that I can bind to the ComboBox. I set up the display name and value members and I also select the item to display where the attribute "selected" has "true".

Now all of your ComboBox objects are bound to the XML file, the next fun part is to save their state.

private void saveButton_Click(object sender, EventArgs e)
{
    var comboBoxData = new XDocument();
    var dataRootElement = new XElement("data");

    foreach (Control control in this.Controls)
    {
        if (control is ComboBox)
        {
            var comboBoxElement = new XElement("comboBox");
            comboBoxElement.SetAttributeValue("id", control.Name);

            for (int index = 0; index < ((ComboBox)control).Items.Count; index++)
            {
                ComboBoxItem item =
                    (ComboBoxItem)((ComboBox)control).Items[index];

                var itemElement = new XElement("item");
                itemElement.SetAttributeValue("displayName", item.ItemDisplayName);
                itemElement.SetAttributeValue("value", item.ItemValue);
                itemElement.SetAttributeValue("selected",
                    ((ComboBox)control).SelectedIndex == index ? "true" : "false");

                comboBoxElement.Add(itemElement);
            }

            dataRootElement.Add(comboBoxElement);
        }
    }

    comboBoxData.Add(dataRootElement);
    comboBoxData.Save("ComboBoxData.xml");
}



This is actually easier then it sounds. I use the XDocument and XElement objects to construct each XML element for the document. I use attributes to store the state of the object.

Hopefully this gives you a good start!


这篇关于从xml读取时,Combobox只更新一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆