foreach (xmlNode) 只插入第一个值
foreach (xmlNode) only inserts the first value
我正在尝试使用 Web 服务将 MS Infopath
重复 table 中的值插入到 SQL server DB
中,但只有第一个值被插入和复制,具体取决于数量行有。
InfoPath
SQL 服务器数据库
代码
[WebMethod]
public void SubmitDocument(XmlDocument doc)
{
string firstname;
string lastname;
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-08-16T17:55:49");
nsManager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
XmlNode root = doc.DocumentElement;
XmlNodeList list = root.SelectNodes("/dfs:IPDocument/my:myFields/my:UserList/my:Users", nsManager);
foreach (XmlNode node in list)
{
firstname = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:UserList/my:Users/my:FirstName", nsManager).InnerText;
lastname = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:UserList/my:Users/my:LastName", nsManager).InnerText;
SubmitToDatabase(firstname, lastname);
}
}
public void SubmitToDatabase(string firstname, string lastname)
{
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO infopathconcepttable(FirstName, LastName)VALUES(@FirstName,@LastName)", conn);
cmd.Parameters.AddWithValue("@FirstName", firstname);
cmd.Parameters.AddWithValue("@LastName", lastname);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (SqlException ex)
{
}
}
我认为这与在您的 SelectSingleNode()
调用中提供绝对 XPath 表达式有关。
尝试用以下行替换两个 SelectSingleNode()
调用:
firstname = node["FirstName", nsManager.LookupNamespace("my")].InnerText;
lastname = node["LastName", nsManager.LookupNamespace("my")].InnerText;
考虑到您已经访问了您的 XmlNodeList,您不需要在循环中重新指定您希望获取的节点。
如果您确定不会在源代码中获得空值,则类似以下的内容应该会为您提供预期的结果:
foreach (XmlNode node in list)
{
firstname = node.SelectSingleNode("FirstName").InnerText;
lastname = node.SelectSingleNode("LastName").InnerText;
SubmitToDatabase(firstname, lastname);
}
您还可以通过索引访问节点列表中的节点:
firstname = node["FirstName"].InnerText;
lastname = node["LastName"].InnerText;
我正在尝试使用 Web 服务将 MS Infopath
重复 table 中的值插入到 SQL server DB
中,但只有第一个值被插入和复制,具体取决于数量行有。
InfoPath
SQL 服务器数据库
代码
[WebMethod]
public void SubmitDocument(XmlDocument doc)
{
string firstname;
string lastname;
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-08-16T17:55:49");
nsManager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
XmlNode root = doc.DocumentElement;
XmlNodeList list = root.SelectNodes("/dfs:IPDocument/my:myFields/my:UserList/my:Users", nsManager);
foreach (XmlNode node in list)
{
firstname = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:UserList/my:Users/my:FirstName", nsManager).InnerText;
lastname = node.SelectSingleNode("/dfs:IPDocument/my:myFields/my:UserList/my:Users/my:LastName", nsManager).InnerText;
SubmitToDatabase(firstname, lastname);
}
}
public void SubmitToDatabase(string firstname, string lastname)
{
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO infopathconcepttable(FirstName, LastName)VALUES(@FirstName,@LastName)", conn);
cmd.Parameters.AddWithValue("@FirstName", firstname);
cmd.Parameters.AddWithValue("@LastName", lastname);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (SqlException ex)
{
}
}
我认为这与在您的 SelectSingleNode()
调用中提供绝对 XPath 表达式有关。
尝试用以下行替换两个 SelectSingleNode()
调用:
firstname = node["FirstName", nsManager.LookupNamespace("my")].InnerText;
lastname = node["LastName", nsManager.LookupNamespace("my")].InnerText;
考虑到您已经访问了您的 XmlNodeList,您不需要在循环中重新指定您希望获取的节点。 如果您确定不会在源代码中获得空值,则类似以下的内容应该会为您提供预期的结果:
foreach (XmlNode node in list)
{
firstname = node.SelectSingleNode("FirstName").InnerText;
lastname = node.SelectSingleNode("LastName").InnerText;
SubmitToDatabase(firstname, lastname);
}
您还可以通过索引访问节点列表中的节点:
firstname = node["FirstName"].InnerText;
lastname = node["LastName"].InnerText;