CodeGo.net>如何读取XML到DataGrid的组合框?

我正在尝试将xml文件读取到数据集,并将数据集绑定到datagrid

XML档案:

<NewDataSet>
    <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>BaudRate</ParamName>
        <ParamValues>
            <ParamValue>9600</ParamValue>
            <ParamValue>19200</ParamValue>
            <ParamValue>115200</ParamValue>
        </ParamValues>
        <DefaultValue>19200</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
    <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>Parity</ParamName>
        <ParamValues>
            <ParamValue>None</ParamValue>
            <ParamValue>Odd</ParamValue>
            <ParamValue>Even</ParamValue>
        </ParamValues>
        <DefaultValue>None</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
        <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>StopBit</ParamName>
        <ParamValues>
           <ParamValue>1</ParamValue>
           <ParamValue>2</ParamValue>
        </ParamValues>
        <DefaultValue>1</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
    </Communications>
    <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>DataBit</ParamName>
        <ParamValues>
           <ParamValue>7</ParamValue>
           <ParamValue>8</ParamValue>
        </ParamValues>
        <DefaultValue>8</DefaultValue>
        <MaxValue></MaxValue>
        <MinValue></MinValue>
   </Communications>
   <Communications>
        <ModelNumber>1</ModelNumber>
        <ParamName>SlaveAddress</ParamName>
        <ParamValues>
            <ParamValue>1</ParamValue>
        </ParamValues>
        <DefaultValue>1</DefaultValue>
        <MaxValue>247</MaxValue>
        <MinValue>1</MinValue>
   </Communications>
</NewDataSet>

将xml读取到数据集:

    public ObservableCollection<Communication> GetCommunications()
    {
        DataSet ds = new DataSet();
        ds.ReadXml("Communications.xml");
        ObservableCollection<Communication> communications = new ObservableCollection<Communication>();
        foreach (DataRow communicationRow in ds.Tables["Communications"].Rows)
        {
            var c = new Communication((ushort)Convert.ToInt16(communicationRow["ModelNumber"]), communicationRow["ParamName"].ToString(),
                    ds.Tables["ParamValue"].Rows[0][0].ToString(), communicationRow["DefaultValue"].ToString(), communicationRow["MaxValue"].ToString(),
                    communicationRow["MinValue"].ToString());
            foreach (DataRow dr in ds.Tables["ParamValue"].Rows)
            {
                c.ParamValues.Add(dr[0].ToString());
            }
            communications.Add(c);
        }
        return communications;
    }

绑定Datagrid:

    <DataGridTemplateColumn Header="ParamValues">                                              
       <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                 <ComboBox ItemsSource="{Binding ParamValues}" SelectedItem="{Binding DefaultValue}" />
            </DataTemplate>                                                           
       </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

但是上面的代码带有以下错误:组合框ParamValues的每一列都具有表Communications Image detail的所有ParamValue,请您告诉我如何解决此问题?谢谢!

解决方法:

用XDocument替换数据集:

public ObservableCollection<Communication> GetCommunications()
{
    ObservableCollection<Communication> communications = new ObservableCollection<Communication>();
    XDocument doc = XDocument.Load("Communications.xml");
    foreach (XElement communicationRow in doc.Root.Elements("Communications"))
    {
        var c = new Communication((ushort)Convert.ToInt16(communicationRow.Element("ModelNumber").Value), communicationRow.Element("ParamName").Value,
                communicationRow.Element("DefaultValue").Value, communicationRow.Element("DefaultValue").Value, communicationRow.Element("MaxValue").Value,
                communicationRow.Element("MinValue").Value);

        foreach (XElement paramValue in communicationRow.Element("ParamValues").Elements())
        {
            c.ParamValues.Add(paramValue.Value);
        }

        communications.Add(c);
    }

    return communications;
}
上一篇:c语言实现扫雷


下一篇:集群中用Memcached来实现session共享