我有一个 XML 模式 xsd,我使用 Visual Studio 中的 Xsd2Code 工具将其转换为 cs 文件,
我目前正在做的是, //请按照手写方式忽略语法
Public Method_1()
{
Fruits f = new Fruits();
f.Name = "Mangoes";
Foreach (DataRow row in DataTable) //assume i am getting rows from database
{
FruitsCrate fc = new FruitsCrate();
fc.Id = 2;
fc..... etc
f.FruitsCrate.add(fc); // as fruitCrate is a child node of fruit
}
Foreach (DataRow row in DataTable)
{
FruitBasket fb = new FruitBasket();
fb.id = 2;
fb.... etc
f.FruitBaskets.add(fb); // as fruit basket is child node
}
//a lot of more foreach just like above
}
现在的问题是我想将 foreach 语句放在不同的方法中,所以我不确定如何将不同的 childNodes 添加到父节点,
我想做的是,
Public Method_1() //hand written code so syntax might not be right
{
Fruits f = new Fruits();
f = Method-2(); // ???????????????
f = Method-3();
f = Method-4(); //........... won't f value will be reset each time
}
public Fruits Method_2(Fruits f)
{
foreach(dataRow row in DataTable)
{
FruitCrate fc = new FruitCrate();
fc.propertyA = "aaaa"; etc...
f.FruitsCrate.add(fc);
}
return f;
}
第二种编码方式是手写的,我认为行不通,甚至是不可能的方式