我正在读取XML文件并根据XML文件的元素保存提取的数据。 我正在使用XmlReader。 但是当我运行程序时...它运行得很精细但是只有NULL值被发送到我的数据库中。 我的代码如下:
using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
string org_id;
string org_desig;
string org_name;
string add_1;
string add_2;
string add_3;
string cityname;
string countrycode;
string countryname;
string postalcode;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Load the XML file
XmlTextReader reader = new XmlTextReader("PXMLF-8612013050420130606105906.xml");
// Loop over the XML file
while (reader.Read())
{
// look for element
if (reader.NodeType == XmlNodeType.Element)
{
// If the element is the one required
if (reader.Name == "OrganizationID")
{
org_id = (reader.ReadElementString());
}
if (reader.Name == "OrganisationDesignator")
{
org_desig = (reader.ReadElementString());
}
if (reader.Name == "OrganizationName1")
{
org_name = (reader.ReadElementString());
}
if (reader.Name == "AddressLine1")
{
add_1 = (reader.ReadElementString());
}
if (reader.Name == "AddressLine2")
{
add_2 = (reader.ReadElementString());
}
if (reader.Name == "AddressLine3")
{
add_3 = (reader.ReadElementString());
}
if (reader.Name == "CityName")
{
cityname = (reader.ReadElementString());
}
if (reader.Name == "CountryCode")
{
countrycode = (reader.ReadElementString());
}
if (reader.Name == "CountryName")
{
countryname = (reader.ReadElementString());
}
if (reader.Name == "PostalCode")
{
postalcode = (reader.ReadElementString());
}
//populate table data from extracted values
//connect to db
string connStr = ConfigurationManager.ConnectionStrings["connXML"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
con.Open();
//send extracted data to db
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into INV_HEADER VALUES ('" + org_id + "','" + org_desig + "', '" + org_name + "' , '" + add_1 + "', '" + add_2 + "', '" + add_3 + "', '" + cityname + "', '" + countrycode + "', '" + countryname + "', '" + postalcode + "')";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
reader.Close();
}
}
答案 0 :(得分:0)
看起来像reader.Read()方法只循环一次迭代。为什么不在调试模式下运行它并查看变量是否实际设置为从Xml节点检索的值?如果他们正确地获取值,那么接下来要做的就是检查你的CommandText。从CommandText复制SQL语句并将其粘贴到SQL Server Management Studio并运行它以查看它是否实际插入记录(假设您使用的是SQL Server)。
答案 1 :(得分:0)
似乎您在每个reader.Read()之后插入值。尝试在while循环后插入。
using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
string org_id;
string org_desig;
string org_name;
string add_1;
string add_2;
string add_3;
string cityname;
string countrycode;
string countryname;
string postalcode;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Load the XML file
XmlTextReader reader = new XmlTextReader("PXMLF-8612013050420130606105906.xml");
// Loop over the XML file
while (reader.Read())
{
// look for element
if (reader.NodeType == XmlNodeType.Element)
{
// If the element is the one required
switch (reader.Name)
{
case "OrganizationID":
org_id = (reader.ReadElementString());
break;
case "OrganisationDesignator":
org_desig = (reader.ReadElementString());
break;
case "OrganizationName1":
org_name = (reader.ReadElementString());
break;
case "AddressLine1":
add_1 = (reader.ReadElementString());
break;
case "AddressLine2":
add_2 = (reader.ReadElementString());
break;
case "AddressLine3":
add_3 = (reader.ReadElementString());
break;
case "CityName":
cityname = (reader.ReadElementString());
break;
case "CountryCode":
countrycode = (reader.ReadElementString());
break;
case "CountryName":
countryname = (reader.ReadElementString());
break;
case "PostalCode":
postalcode = (reader.ReadElementString());
break;
}
}
}
//connect to db
string connStr = ConfigurationManager.ConnectionStrings["connXML"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
con.Open();
//send extracted data to db
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into INV_HEADER VALUES ('" + org_id + "','" + org_desig + "', '" + org_name +
"' , '" + add_1 + "', '" + add_2 + "', '" + add_3 + "', '" + cityname + "', '" +
countrycode + "', '" + countryname + "', '" + postalcode + "')";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
reader.Close();
}
}