我正在阅读xml。从这个xml中,我将4个参数存储到数组中。
如何将这些数组组合成一个foreach循环,以便可以将这些数据存储到SQL中并使用它们?
我设法为两个数组创建.Zip。是否可以像这样组合4个数组?基本上,我想将 id 和 recDate 添加到numbersAndWords数组中。
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
var cislo = new List<string>();
var zprava = new List<string>();
var id = new List<string>();
var recDate = new List<string>();
XmlNodeList number = doc.GetElementsByTagName("SenderNumber");
for (int i = 0; i < number.Count; i++)
{
cislo.Add(number[i].InnerXml);
}
XmlNodeList text = doc.GetElementsByTagName("TextDecoded");
for (int i = 0; i < text.Count; i++)
{
zprava.Add(text[i].InnerXml);
}
XmlNodeList idNum = doc.GetElementsByTagName("ID");
for (int i = 0; i < idNum.Count; i++)
{
id.Add(idNum[i].InnerXml);
}
XmlNodeList recDateTime = doc.GetElementsByTagName("ReceivingDateTime");
for (int i = 0; i < recDateTime.Count; i++)
{
recDate.Add(recDateTime[i].InnerXml);
}
var numbersAndWords = cislo.Zip(zprava, (n, w) => new {Number = n, Word = w});
cs.Open();
foreach (var nw in numbersAndWords)
{
MessageBox.Show(nw.Number);
MessageBox.Show(nw.Word);
}
答案 0 :(得分:1)
尝试使用以下方法组合这些数组:
private static T[] Combine<T>(params IEnumerable<T>[] items) =>
items.SelectMany(i => i).Distinct().ToArray();
并使用它:
var combinedArrays = Combine(cislo, zprava, id, recDate);
for (int i = 0; i < combinedArrays.Length; i++)
{
MessageBox.Show(i);
MessageBox.Show(combinedArrays[i]);
}
如果您要在中执行sql语句,则创建新类并用数据填充它会很好。例如:
public class Item
{
public string Id { get; set; }
public string Cislo { get; set; }
public string Zprava { get; set; }
public string RecDate { get; set; }
}
var items = id.Select((id, index) => new Item()
{
Id = id,
Cislo = cislo[index],
Zprava = zprava[index],
RecDate = recDate[index]
}).ToList();
foreach (var item in items)
{
string sql = " insert into contacts(id, zprava, cislo) values (@id, @zprava, @cislo)";
var command = new SqlCommand(sql, connection);
// bind the parameters in the sql based on the `item` object.
// execute the command, etc..
}
答案 1 :(得分:0)
如果您确定所有元素数均相等,请创建类并填写:
class Record
{
public string cislo;
public string zprava;
public string id;
public string recDate;
}
.....
List<Record> records = new List<Record>();
XmlNodeList number = doc.GetElementsByTagName("SenderNumber");
for (int i = 0; i < number.Count; i++)
{
records.Add(new Record { cislo = number[i].InnerXml });
}
XmlNodeList text = doc.GetElementsByTagName("TextDecoded");
for (int i = 0; i < text.Count; i++)
{
records[i].zprava = text[i].InnerXml;
}
等
答案 2 :(得分:0)
您可以使用Zip
的实现来压缩四个可枚举,并返回一个ValueTuple<T, T, T, T>
元素的可枚举:
public static IEnumerable<(T, T, T, T)> Zip4<T>(IEnumerable<T> source1,
IEnumerable<T> source2,
IEnumerable<T> source3,
IEnumerable<T> source4)
{
using (IEnumerator<T> e1 = source1.GetEnumerator(),
e2 = source2.GetEnumerator(),
e3 = source3.GetEnumerator(),
e4 = source4.GetEnumerator())
{
while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext() && e4.MoveNext())
{
yield return (e1.Current, e2.Current, e3.Current, e4.Current);
}
}
}
用法示例:
foreach (var combo in Zip4(cislo, zprava, id, recDate))
{
Console.WriteLine($"{combo.Item1}-{combo.Item2}-{combo.Item3}-{combo.Item4}");
}