使用Zip组合3个字符串而不是2个字符串

时间:2014-01-31 18:41:35

标签: c# xml

我正在撰写Function以将新Nodes添加到XML文件,因此我创建了2 List<string>,然后将它们合并为以下

var FirstText = FirstList;
var SecondText = SecondList;
var AllText = FirstText.Zip(SecondText, (t1, t2) => new { FirstWord = t1, SecondWord = t2 });
foreach (var tt in AllText)
{
    Xml.WriteStartElement(tt.FirstWord);
    Xml.WriteValue(tt.SecondWord);
    Xml.WriteEndElement();
}

添加类似这样的内容

<Force>True</Force>
<Auto>False</Auto>

现在我想通过写这个

来组合3 strings而不是2
Xml.WriteStartElement(tt.FirstWord);
Xml.WriteStartElement(tt.SecondWord);
Xml.WriteValue(tt.ThirdWord);
Xml.WriteEndElement();
Xml.WriteEndElement();

所以XML看起来像

<Settings>
  <Force>True</Force>
</Settings>
<Settings>
  <Auto>True</Auto>
</Settings>

我该怎么做?

1 个答案:

答案 0 :(得分:3)

再次致电.Zip

var AllText = FirstText.Zip(SecondText, (t1, t2) => new { FirstWord = t1, SecondWord = t2 });
                       .Zip(Values, (t12, v) => new 
                           { FirstWord = t12.FirstWord, 
                             SecondWord = t12.SecondWord,
                             ThirdWord = v });

你也可以