gpt4 book ai didi

c# foreach 没有在循环中获取下一个 XElement 名称

转载 作者:数据小太阳 更新时间:2023-10-29 02:46:33 25 4
gpt4 key购买 nike

我有以下代码应该执行以下操作:

如果有从web服务返回的xml格式的记录,循环遍历每条记录,得到ErrorCode、ErrorMessage和ID。通过电子邮件发送该信息。然后解析出每条记录并将子元素写入一个表。问题是它写入同一条记录的次数与记录的数量一样多。我似乎无法弄清楚为什么。电子邮件部分按预期工作。

代码如下:

#region If There Are RecordsInError From Broker Acknowledgement
if (srca.NumberOfResponseRecordsInError != "0")
{
int TotalCount ;
int.TryParse(srca.NumberOfResponseRecordsInError,out TotalCount);
string[] ErrorCode = new string[TotalCount];
string[] ErrorMessage = new string[TotalCount];
string EmailBody = null;
string StateRequestRecordGUID = null;

for (int i = 0; i < TotalCount; i++)
{
//ZJR: Populate the string arrays with Error
ErrorCode[i] = srca.FailedSeparationResponse[i].ErrorOccurrence[0].ErrorCode;
ErrorMessage[i] = srca.FailedSeparationResponse[i].ErrorOccurrence[0].ErrorMessage;
StateRequestRecordGUID = srca.FailedSeparationResponse[i].StateRequestRecordGUID;

//ZJR: Database connection to insert Error Records
SqlConnection conn4 = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=test_BdbCSSQL01;Persist Security Info=False;Integrated Security=SSPI;");
conn4.Open();

string sql = "SELECT * FROM ERROROfSIDESStagingOUT";
SqlDataAdapter da = new SqlDataAdapter(sql, conn4);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);

XDocument doc = XDocument.Load("XmlString.xml");
XNamespace ns = "https://uidataexchange.org/schemas";

var node = doc.Descendants(ns + "EmployerTPASeparationResponse");

using (da)
{
foreach (var param in node.Elements())
{
try
{
if (dr.Table.Columns.Contains(param.Name.LocalName))
{
dr[param.Name.LocalName] = param.Value;
}
//param.NextNode;
}
catch (Exception ee)
{
string asdf = ee.ToString();
}
}
SqlCommandBuilder sb = new SqlCommandBuilder(da);
da.Update(dt);
}

if (conn4 != null)
{
conn4.Close();
}

//ZJR: Build the Email Body
EmailBody = EmailBody + "StateRequestRecordGUID: " + StateRequestRecordGUID + Environment.NewLine + "ErrorCode: " + ErrorCode[i] + " : " + ErrorMessage[i] + Environment.NewLine + Environment.NewLine;
SendEmail mail = new SendEmail();
mail.Send(EmailBody, "Sides Error Event");
}
}
#endregion

最佳答案

我发现了问题。这是 using (da) 行。我摆脱了它,代码运行良好。

关于c# foreach 没有在循环中获取下一个 XElement 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5983279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com