Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加到列表时,列表中的元素将被覆盖_C#_.net_List - Fatal编程技术网

C# 添加到列表时,列表中的元素将被覆盖

C# 添加到列表时,列表中的元素将被覆盖,c#,.net,list,C#,.net,List,我正在从XML文件中读取标签和值,以获取功能测试软件所需的信息。将频率添加到列表时,所有其他现有列表项的频率将被新频率覆盖 public struct Configuration { public string Description; public byte Mode; public byte Deviation; public string PartNumber; public UInt16[] Frequencies; public byt

我正在从XML文件中读取标签和值,以获取功能测试软件所需的信息。将频率添加到列表时,所有其他现有列表项的频率将被新频率覆盖

public struct Configuration 
{
    public string Description;
    public byte Mode;
    public byte Deviation;
    public string PartNumber;
    public UInt16[] Frequencies;
    public byte TxPower;
    public byte PWM;
    public Int16 HeaterSetpoint;
    public string FirmwareVersion;
    public bool Active;
    public int Threshold;
} // struct Configuration

Configurations = new List<Configuration>();
ushort[] Frequencies = new ushort [7];
Configuration TestConfig = new Configuration();

/*Open the XML file, load all the tag values into XmlNodeList */
//Pull in all the XML Tag Values Sorted by Tag Name
XmlNodeList xml_description = ConfigFile.GetElementsByTagName("description");
XmlNodeList xml_mode = ConfigFile.GetElementsByTagName("mode");
XmlNodeList xml_deviation = ConfigFile.GetElementsByTagName("deviation");
XmlNodeList xml_partnumber = ConfigFile.GetElementsByTagName("partnumber");
XmlNodeList xml_channel0 = ConfigFile.GetElementsByTagName("channel0");
XmlNodeList xml_channel1 = ConfigFile.GetElementsByTagName("channel1");
XmlNodeList xml_channel2 = ConfigFile.GetElementsByTagName("channel2");
XmlNodeList xml_channel3 = ConfigFile.GetElementsByTagName("channel3");
XmlNodeList xml_channel4 = ConfigFile.GetElementsByTagName("channel4");
XmlNodeList xml_channel5 = ConfigFile.GetElementsByTagName("channel5");
XmlNodeList xml_channel6 = ConfigFile.GetElementsByTagName("channel6");
XmlNodeList xml_txpower = ConfigFile.GetElementsByTagName("txpower");
XmlNodeList xml_pwm = ConfigFile.GetElementsByTagName("pwm");
XmlNodeList xml_hsp = ConfigFile.GetElementsByTagName("hsp");
XmlNodeList xml_firmware = ConfigFile.GetElementsByTagName("firmware");
XmlNodeList xml_active = ConfigFile.GetElementsByTagName("active");
XmlNodeList xml_threshold = ConfigFile.GetElementsByTagName("threshold");

/*Use LINQ to check that the XmlNodeLists are all the same length. This way we don't try and load invalid data*/

if(!listsaresamelength)
{
    /*Alert user there is a problem with the XML file and close the application*/
}

//Loop to add values to the List
for(i = 0; i < NumberofXmlValues; i++)
{
    /*check that the description, Mode, Deviation are valid and add them to DummyConfig*/

    try
    {
        Frequencies[0] = UInt16.Parse(xml_channel0[i].InnerText);
        Frequencies[1] = UInt16.Parse(xml_channel1[i].InnerText);
        Frequencies[2] = UInt16.Parse(xml_channel2[i].InnerText);
        Frequencies[3] = UInt16.Parse(xml_channel3[i].InnerText);
        Frequencies[4] = UInt16.Parse(xml_channel4[i].InnerText);
        Frequencies[5] = UInt16.Parse(xml_channel5[i].InnerText);
        Frequencies[6] = UInt16.Parse(xml_channel6[i].InnerText);
        //Move the frequencies to the list
        TestConfig.Frequencies = Frequencies;
    }

    catch 
    {
        /*Problem with the XML file. Alert the user and close the application*/
    }

    /*check that the heater set point, PWM set point, partnumber, Txpower, and Threshold are valid and add them to DummyConfig*/

    Configurations.Add(TestConfig)

}
公共结构配置
{
公共字符串描述;
公共字节模式;
公共字节偏差;
公共字符串部件号;
公共UInt16[]频率;
公共字节功率;
公共字节脉宽调制;
公共Int16加热器设定值;
公共字符串FirmwareVersion;
公共图书馆活跃;
公共整数阈值;
}//结构配置
配置=新列表();
ushort[]频率=新ushort[7];
Configuration TestConfig=新配置();
/*打开XML文件,将所有标记值加载到XmlNodeList中*/
//拉入按标记名排序的所有XML标记值
XmlNodeList xml_description=ConfigFile.GetElementsByTagName(“description”);
XmlNodeList xml_mode=ConfigFile.GetElementsByTagName(“mode”);
XmlNodeList xml_deviation=ConfigFile.GetElementsByTagName(“偏差”);
XmlNodeList xml_partnumber=ConfigFile.GetElementsByTagName(“partnumber”);
XmlNodeList xml_channel0=ConfigFile.GetElementsByTagName(“channel0”);
XmlNodeList xml_channel1=ConfigFile.GetElementsByTagName(“channel1”);
XmlNodeList xml_channel2=ConfigFile.GetElementsByTagName(“channel2”);
XmlNodeList xml_channel3=ConfigFile.GetElementsByTagName(“channel3”);
XmlNodeList xml_channel4=ConfigFile.GetElementsByTagName(“channel4”);
XmlNodeList xml_channel5=ConfigFile.GetElementsByTagName(“channel5”);
XmlNodeList xml_channel6=ConfigFile.GetElementsByTagName(“channel6”);
XmlNodeList xml_txpower=ConfigFile.GetElementsByTagName(“txpower”);
XmlNodeList xml_pwm=ConfigFile.GetElementsByTagName(“pwm”);
XmlNodeList xml_hsp=ConfigFile.GetElementsByTagName(“hsp”);
XmlNodeList xml_firmware=ConfigFile.GetElementsByTagName(“固件”);
XmlNodeList xml_active=ConfigFile.GetElementsByTagName(“active”);
XmlNodeList xml_threshold=ConfigFile.GetElementsByTagName(“threshold”);
/*使用LINQ检查XMLNodeList的长度是否都相同。这样我们就不会尝试加载无效数据*/
如果(!listsaresamelength)
{
/*提醒用户XML文件有问题并关闭应用程序*/
}
//循环以向列表中添加值
对于(i=0;i
for循环的第一次迭代运行良好;结果如下:


我尝试了几种不同的方法来改变我写入列表的方式,但它们都产生了相同的结果。我觉得我错过了一些明显的东西

您需要将
TestConfig
Frequencies
变量的实例化移动到
for
循环中,以便每次都使用不同的对象

for(i = 0; i < NumberofXmlValues; i++)
{
    TestConfig = new Configuration();
    Frequencies = new ushort [7];
(i=0;i { TestConfig=新配置(); 频率=新的ushort[7]; 我个人建议使用这些局部声明的变量,而不是持久性字段。这种模式将帮助您避免将来出现类似的错误

for(i = 0; i < NumberofXmlValues; i++)
{
    /*check that the description, Mode, Deviation are valid*/
    ...
    try
    {
        var testConfig = new Config
        {
            Description = description
            ...
            Frequencies = new [] {xml_channel0, xml_channel1, ...}
               .Select(c => UInt16.Parse(c[i].InnerText))
               .ToArray()
        };
        Configurations.Add(testConfig);
    }
    ...
(i=0;i { /*检查说明、模式、偏差是否有效*/ ... 尝试 { var testConfig=new Config { 描述=描述 ... 频率=新[]{xml_channel0,xml_channel1,…} .Select(c=>UInt16.Parse(c[i].InnerText)) .ToArray() }; 添加(testConfig); } ...
您需要将
TestConfig
Frequencies
变量的实例化移动到
for
循环中,以便每次使用不同的对象

for(i = 0; i < NumberofXmlValues; i++)
{
    TestConfig = new Configuration();
    Frequencies = new ushort [7];
(i=0;i { TestConfig=新配置(); 频率=新的ushort[7]; 我个人建议使用这些局部声明的变量,而不是持久性字段。这种模式将帮助您避免将来出现类似的错误

for(i = 0; i < NumberofXmlValues; i++)
{
    /*check that the description, Mode, Deviation are valid*/
    ...
    try
    {
        var testConfig = new Config
        {
            Description = description
            ...
            Frequencies = new [] {xml_channel0, xml_channel1, ...}
               .Select(c => UInt16.Parse(c[i].InnerText))
               .ToArray()
        };
        Configurations.Add(testConfig);
    }
    ...
(i=0;i { /*检查说明、模式、偏差是否有效*/ ... 尝试 { var testConfig=new Config { 描述=描述 ... 频率=新[]{xml_channel0,xml_channel1,…} .Select(c=>UInt16.Parse(c[i].InnerText)) .ToArray() }; 添加(testConfig); } ...
“将频率添加到列表时”-您正在将
DummyConfig
添加到列表中,但我们不知道这是什么。您没有在循环的其余部分使用它。但是,另外,您在循环的每个迭代中都使用相同的数组-您从未创建新数组。我输入了一个错误的数组。我将尝试创建新数组。不过,想想看,这似乎是错误的有更好的解决办法吗?如果有关系的话,我也会从别人留下的地方开始。我打破了一个习惯