I've made my own tree data structure via classes.
Now I'm stuck with really basic stuffs. I need to make output tab delimited file from data in my List <MainTreeNode>
.
I think that recursion is only way?!
Tree is N-tree, and output have first row as header and other rows are values.
Tree:
- MSG (MainTreeNode)
- MainParam (Must have prop NAME, doesn't have to have prop VALUE)
- SubParam1 (Must have prop NAME, must have prop VALUE)
- SubParam2 (Must have prop NAME, doesn't have to have prop VALUE)
- SubSubParam2.1 (Must have prop NAME, must have prop VALUE)
etc.
Or:
- Message : Name
- Param1 : ValueV1 (VALUE must, because it doesn't have children)
- Param2
- Param2.1 : ValueV2
- Param2.2 : Value
- Param2.2.1 : ValueV3
- Param2.2.2 : ValueV4 ...etc.
And output have to be like this (first line is header):
Param1|Param2/Param2.1|Param2/Param2.2/Param2.2.1|Param2/Param2.2/Param2.2.2
ValueV1|ValueV2|ValueV3|ValueV4
...
So I need probably List for header and for values but I don't know how to implement that in recursion way (or any another).
Some of unfinished code:
public void PrintToTabFile(List<Message> messages, List<string> parameters)
{
foreach (string s in parameters)
{
using (StreamWriter streamWriter = new StreamWriter(@"C: emp" + s + ".xls"))
{
streamWriter.Write("No. MsgName MsgData1 MsgData2 MsgData3");
var msg = messages.Where(x => x.Parameters.Where(p => p.ParameterName == s).Count() == 1);
List<string> headers = new List<string>();
List<string> values= new List<string>();
//... Stuck!!!
}
}
}
private void Recursion(Parameter parameter, List<string> headers, List<string> values)
{
if (parameter.SubParameters.Count == 0)
{
int index = headers.IndexOf(parameter.ParameterName);
values[index] = parameter.ParameterValue;
}
else
{
foreach (Parameter p in parameter.SubParameters)
{
Recursion(p, headers, values);
//and Stuck Probably here or before recursion call
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…