C# 列表新手:xml元素和域对象的列表在每次迭代中都会被覆盖

C# 列表新手:xml元素和域对象的列表在每次迭代中都会被覆盖,c#,c#-4.0,xml-parsing,C#,C# 4.0,Xml Parsing,我有两个域类,如下所示: Class FooA: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace XXX.MyCore.Domain { public class FooA { public string String_FA { get; set; } public string String_FB {

我有两个域类,如下所示:

Class FooA:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XXX.MyCore.Domain
{
  public class FooA
    {
        public string String_FA { get; set; } 
        public string String_FB { get; set; }

    }
}

Class FooB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XXX.MyCore.Domain
{
    public class FooB
    {
        public string FooC{ get; set; }
        public List<FooA> FooA_List { get; set; }

    }
}

被覆盖

因此,当循环完成时,每个列表元素被复制6倍 所以

非常感谢您的帮助


谢谢

首先,您希望在每个文档中实例化一个新的FooA。其次,没有理由每次都重置列表,您可以使用现有列表。尝试以下更改:

// Create a new list and assign it to the public property of FooB...
FooB.FooA_List = new List<FooA>();

foreach (XElement elem in document.Descendants().Elements(nsUsr + "ExampleA"))
{
    // Create a temporary variable (in the scope of this loop iteration) to store my new FooA class instance...
    FooA fA = new FooA() { 
        String_FA = elem.Element("A").Value, 
        String_FB = elem.Element("B").Value
    };

    // Because FooB.FooA_List is the list I want to add items to, I just access the public property directly.
    FooB.FooA_List.Add(fA);
}
//创建一个新列表并将其分配给FooB的公共属性。。。
FooB.FooA_List=新列表();
foreach(document.subjections().Elements(nsUsr+“ExampleA”)中的XElement元素)
{
//创建一个临时变量(在此循环迭代的范围内)来存储我的新FooA类实例。。。
FooA fA=新的FooA(){
字符串_FA=元素(“A”)值,
字符串_FB=元素(“B”)值
};
//因为FooB.FooA_List是我想要添加项目的列表,所以我只需直接访问public属性。
FooB.FooA_List.Add(fA);
}
创建一个全新的列表,然后将该列表分配给
FooA
的属性,这是一项额外的工作
fA
是仅存在于当前循环范围内的实例,只要循环转到下一个循环,
fA
将自动成为全新的,就好像它从未存在过一样


FooB.FooA\u List
是您正在添加内容的列表实例。不断地将此变量重新分配给列表实例的不同副本是没有意义的。因此,无需在循环中使用
FooB.FooA_List=whatever
,因为您可以通过
FooB.FooA_List
直接访问实例,并通过
FooB.FooA_List.Add(whatever)使其完成工作

我发现了问题所在。 1.我需要在循环中实例化fA对象。 2.我需要在循环中将fA对象设置为null

foreach (XElement elem in document.Descendants().Elements(nsUsr + "ExampleA"))
{
FooA fA = new FooA();                             
fA.String_FA= elem.Element("A").Value;
fA.String_FB= elem.Element("B").Value;

fooNodeElemValue.Add(fA);
FooB.FooA_List= fooNodeElemValue;
fA =null;
}

谢谢,在查看您的回复之前,我就知道了。感谢您的回复。实际上,在循环结束时,您不需要将其设置为null。一旦一个循环完成,所有的作用域变量(其中fA是一个)都将被描述。此外,您不需要每次在循环中都设置
FooB.FooA\u List=
任何内容。它已经设置过一次,现在您可以通过
FooB.FooA_List.Add(fA)引用并更新它您的代码比需要的更混乱。我会用更好的解释来更新我的答案。
fooNodeElemValue.Add(fA);
FooB.FooA_List= fooNodeElemValue;
FooB.FooA_List[0] = {DataA2, DataB2}
FooB.FooA_List[1] = {DataA2, DataB2}
              :
              :
// Create a new list and assign it to the public property of FooB...
FooB.FooA_List = new List<FooA>();

foreach (XElement elem in document.Descendants().Elements(nsUsr + "ExampleA"))
{
    // Create a temporary variable (in the scope of this loop iteration) to store my new FooA class instance...
    FooA fA = new FooA() { 
        String_FA = elem.Element("A").Value, 
        String_FB = elem.Element("B").Value
    };

    // Because FooB.FooA_List is the list I want to add items to, I just access the public property directly.
    FooB.FooA_List.Add(fA);
}
foreach (XElement elem in document.Descendants().Elements(nsUsr + "ExampleA"))
{
FooA fA = new FooA();                             
fA.String_FA= elem.Element("A").Value;
fA.String_FB= elem.Element("B").Value;

fooNodeElemValue.Add(fA);
FooB.FooA_List= fooNodeElemValue;
fA =null;
}