且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c#:使用Linq获取要遍历的XML节点的IEnumerable集合

更新时间:2023-11-14 09:47:10

您需要在所有元素名称中包括名称空间(xmlns):

You need to include the namespace (xmlns) in all of the element names:

XNamespace ns = "https://uidataexchange.org/schemas";

doc.Descendants(ns + "EmployerTPASeparationResponse")

(在所有Element调用中)

请注意,通过遍历子元素并使用其名称添加参数而不是使用匿名类型,可以大大缩短代码.

Note that your code can be made dramatically shorter by looping through the child elements and adding parameters with their names instead of using anonymous types.

例如:

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

using (cmd4) {
    foreach(var param in node.Elements()) {
        cmd4.Parameters.AddWithValue(param.Name.LocalName, param.Value);
    }
    cmd4.ExecuteNonQuery();
}