0
我有這種情況。我的公司希望我創建這個應用程序,該應用程序從我們從其他來源獲得的XML文件中讀取和插入數據。我堅持的部分是,當我收到的XML文件:將數據插入數據庫的C#/ XML問題
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<queryConfirmation xmlns="http://www.npdb-hipdb.hrsa.gov/QueryResponseCommon">
<submitter xmlns="">
<entityDBID>800000000000001</entityDBID>
<vendorID>1111514</vendorID>
</submitter>
<submissionFilename xmlns="">test.xml</submissionFilename>
<certification xmlns="">
<name>JOE SMITH</name>
<title>SUPERVISOR</title>
<phone>
<number>4075556647</number>
</phone>
<date>2008-02-26</date>
</certification>
<batchStatus xmlns="">
<dcn>2000000000388120</dcn>
<processDate>2014-04-03</processDate>
<successfullyProcessed>false</successfullyProcessed>
<error>
<code>09</code>
<message>09: This entity does not have the privilege to perform this transaction.</message>
</error>
</batchStatus>
</queryConfirmation>
我無法將數據插入到我目前的代碼數據庫,除非我拿出MLNS =「」和xmlns =「HTTP ://www.npdb-hipdb.hrsa.gov/QueryResponseCommon「從XML文件手動。我覺得像服用此信息出來的文件是錯誤的,但下面是我的代碼:
private void insertResponseXml()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML|*.xml";
if (ofd.ShowDialog() == DialogResult.OK)
{
XmlDocument doc = new XmlDocument();
doc.Load(ofd.FileName);
XmlElement xelRoot = doc.DocumentElement;
XmlNodeList rootNodes = xelRoot.SelectNodes("/queryConfirmation");
XmlNodeList submitterNodes = xelRoot.SelectNodes("/queryConfirmation/submitter");
XmlNodeList certNode = xelRoot.SelectNodes("/queryConfirmation/certification");
XmlNodeList certPhoneNode = xelRoot.SelectNodes("/queryConfirmation/certification/phone");
XmlNodeList batchNodeList = xelRoot.SelectNodes("/queryConfirmation/batchStatus");
XmlNodeList bsErrorList = xelRoot.SelectNodes("/queryConfirmation/batchStatus/error");
foreach (XmlNode xndNode in submitterNodes)
{
entityDBID = xndNode["entityDBID"].InnerText;
vendorID = xndNode["vendorID"].InnerText;
foreach (XmlNode submisFilenameNode in rootNodes)
{
submissionFilename = submisFilenameNode["submissionFilename"].InnerText;
}
foreach (XmlNode cfn in certNode)
{
name = cfn["name"].InnerText;
title = cfn["title"].InnerText;
certificationDate = cfn["date"].InnerText;
}
foreach (XmlNode cfnp in certPhoneNode)
{
phoneNumber = cfnp["number"].InnerText;
if (cfnp["extension"] == null)
{
phoneExtension = "null";
}
else
{
phoneExtension = cfnp["extension"].InnerText;
}
}
foreach (XmlNode bsNode in batchNodeList)
{
dcn = bsNode["dcn"].InnerText;
processDate = bsNode["processDate"].InnerText;
successfullyProcessed = bsNode["successfullyProcessed"].InnerText;
}
foreach (XmlNode bsError in bsErrorList)
{
if (bsError["code"] == null)
{
code = "null";
}
else
{
code = bsError["code"].InnerText;
}
if (bsError["message"] == null)
{
message = "null";
}
else
{
message = bsError["message"].InnerText;
}
}
try
{
DA.InsertCommand = new SqlCommand("INSERT INTO response VALUES (@entityDBID, @vendorID, @submissionFilename, @fullName, @title, @number, @extension, @certificationDate, @dcn, @processDate, @successfullyProcessed, @code, @message);", DRDB);
DA.InsertCommand.Parameters.AddWithValue("@entityDBID", SqlDbType.NVarChar).Value = entityDBID;
DA.InsertCommand.Parameters.AddWithValue("@vendorID", SqlDbType.NVarChar).Value = vendorID;
DA.InsertCommand.Parameters.AddWithValue("@submissionFilename", SqlDbType.NVarChar).Value = submissionFilename;
DA.InsertCommand.Parameters.AddWithValue("@fullName", SqlDbType.NVarChar).Value = name;
DA.InsertCommand.Parameters.AddWithValue("@title", SqlDbType.NVarChar).Value = title;
DA.InsertCommand.Parameters.AddWithValue("@number", SqlDbType.NVarChar).Value = phoneNumber;
DA.InsertCommand.Parameters.AddWithValue("@extension", SqlDbType.NVarChar).Value = phoneExtension;
DA.InsertCommand.Parameters.AddWithValue("@certificationDate", SqlDbType.NVarChar).Value = certificationDate;
DA.InsertCommand.Parameters.AddWithValue("@dcn", SqlDbType.NVarChar).Value = dcn;
DA.InsertCommand.Parameters.AddWithValue("@processDate", SqlDbType.NVarChar).Value = processDate;
DA.InsertCommand.Parameters.AddWithValue("@successfullyProcessed", SqlDbType.NVarChar).Value = successfullyProcessed;
DA.InsertCommand.Parameters.AddWithValue("@code", SqlDbType.NVarChar).Value = code;
DA.InsertCommand.Parameters.AddWithValue("@message", SqlDbType.NVarChar).Value = message;
DRDB.Open();
DA.InsertCommand.ExecuteNonQuery();
DRDB.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
而且更糟糕的是,我使用Visual Studio 2005(C#2.5)。任何幫助,將不勝感激。