[英]C# Storing dynamic data with nested lists
I'm trying to figure out the best method of storing dynamic data from an XML file into a C# list with a sublist. 我正在尝试找出将动态数据从XML文件存储到带有子列表的C#列表中的最佳方法。 Each XML document will have 1 or many "Sheet" nodes which will always have attributes for "name" and "data".
每个XML文档将具有1个或多个“ Sheet”节点,这些节点将始终具有“名称”和“数据”的属性。 Each "Sheet" element may have 0 to X number of child "Parameter" nodes which will always have attributes for "ParamName" and "ParamValue".
每个“ Sheet”元素都可以具有0到X个子“ Parameter”节点,这些子节点将始终具有“ ParamName”和“ ParamValue”的属性。
The example data set looks like this: 示例数据集如下所示:
<Root>
<Sheet name="name1" data="This is data for sheet 1">
<Parameters ParamName="PraramName1" ParamValue="ParamValueS1.1">
<Parameters ParamName="PraramName2" ParamValue="ParamValueS1.2">
</Sheet>
<Sheet name="name2" data="This is data for sheet 2">
</Sheet>
<Sheet name="name3" data="This is data for sheet 3">
<Parameters ParamName="PraramName1" ParamValue="ParamValueS3.1">
<Parameters ParamName="PraramName2" ParamValue="ParamValueS3.2">
<Parameters ParamName="PraramName3" ParamValue="ParamValueS3.3">
</Sheet>
</Root>
I am trying to figure out the best method of storing this data into a C# list for each "Sheet" element with an optional sublist for each "Parameters" element. 我正在尝试找出将数据存储到每个“ Sheet”元素的C#列表中以及为每个“ Parameters”元素提供一个可选子列表的最佳方法。
public class SheetData
{
public string SheetName { get; set;}
public string SheetData { get; set;}
}
public class ParameterData
{
public string ParamName { get; set;}
public string ParamValue {get; set;}
}
public class ComboData
{
public SheetData sDat = new SheetData();
public List<ParameterData> pDat = new List<ParameterData>();
}
I'm not sure if I setup the ComboData class correctly or if my issue is with instantiating the class and adding data to it. 我不确定是否正确设置了ComboData类,还是我的问题是实例化该类并向其中添加数据。 I've tried a lot of different scenarios to use this class but haven't been able to get it to work.
我已经尝试了许多不同的场景来使用此类,但是还没有使其能够正常工作。 Examples below.
下面的例子。
List<SheetData> sInfo = new List<SheetData>();
List<ParameterData> pP = new List<ParameterData>();
// This works for the SheetInfo class and also worked when I added a
// reference to a ParameterData list in this class.
sInfo.Add(new SheetInfo
{
SheetName = "SheetName",
SheetQuery = "TheQuery",
});
SheetInfo sIF = new SheetData();
sIF.SheetName = "SName";
sIF.SheetQuery = "SDat";
// This is my most recent failure at adding sheet data with child param data
List<ComboInfo> CombInf = new List<ComboInfo>();
CombInf.Add(new parameters { ParamName = "hth", ParamValue = "dfsf" }, new SheetInfo { SheetName = "dsdfsd", SheetQuery = "sdfsfs" });
foreach (SheetData si in sInfo)
{
Console.WriteLine(si.SheetName);
}
Perhaps I'm approaching this requirement all wrong and shouldn't be trying to use a list or maybe I'm not instantiating the list and sublist correctly. 也许我正在错误地处理此要求,并且不应该尝试使用列表,或者我没有正确实例化该列表和子列表。 Any pointers would be greatly appreciated.
任何指针将不胜感激。
How about combine the classes: 如何合并这些类:
public class Sheet
{
public string SheetName { get; set; }
public string SheetData { get; set; }
public List<ParameterData> ParameterData = new List<ParameterData>();
}
public class ParameterData
{
public string ParamName { get; set; }
public string ParamValue { get; set; }
}
And add just like your xml is formatted: 并添加您的xml格式一样的内容:
sheets.Add(new Sheet()
{
SheetName = "Sheet 1",
SheetData = "FJE35N3LN20",
ParameterData = new List<ParameterData>()
{
new ParameterData()
{
ParamName = "Parameter One",
ParamValue = "1"
},
new ParameterData()
{
ParamName = "Parameter Two",
ParamValue = "2"
}
}
});
Of course this is all hard coded you'll need to use loops when traversing and saving your xml file. 当然,这些都是硬编码,在遍历和保存xml文件时需要使用循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.