我正在尝试使用Web服务将MS Infopath
重复表中的值插入到SQL server DB
中,但是仅第一个值会被插入和复制,具体取决于行数。
InfoPath
SQL Server数据库
代码
[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)
{
}
}
答案 0 :(得分:1)
我相信这与在SelectSingleNode()
调用中提供绝对XPath表达式有关。
尝试用以下几行来代替两个SelectSingleNode()
呼叫:
firstname = node["FirstName", nsManager.LookupNamespace("my")].InnerText;
lastname = node["LastName", nsManager.LookupNamespace("my")].InnerText;
答案 1 :(得分:0)
考虑到您已经访问了XmlNodeList,则无需在循环中重新指定希望获取的节点。 如果您确定在源中不会得到null值,则类似以下的内容应该可以为您带来预期的结果:
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;