我有一个使用控制台c#读取的4GB xml文件。现在,我想从中插入一些数据到数据库。我要查找的节点“ NameDetails”及其子级位于文件中间的某个位置。我编写的代码找不到该节点。如果是抛出错误意外节点 代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
namespace DawJone_Lista
{
class Program
{
static void Main(string[] args)
{
string first_name = " ";
string middle_name = "";
string surname = "";
string gender = "";
string occ_title = "";
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=DawJoneList;Data Source=NLBPRISHTINA\\SQLEXPRESS";
conn.Open();
using (XmlTextReader reader = new XmlTextReader("C:\\Users\\Aplikacionet\\PFA2_201802282200_F.xml"))
{
while (reader.Read())
{
SqlCommand insertCommand = new SqlCommand("spInsertimiListes", conn);
if (reader.IsStartElement("NameDetails"))
{
first_name = " ";
middle_name = " ";
surname = " ";
gender = " ";
occ_title = " ";
while (reader.Read() && reader.IsStartElement())
{
switch (reader.Name)
{
case "FirstName":
first_name = reader.ReadString();
break;
case "MiddleName":
middle_name = reader.ReadString();
break;
case "Surname":
surname = reader.ReadString();
break;
case "Gender":
gender = reader.ReadString();
break;
case "OccTitle":
occ_title = reader.ReadString();
break;
default:
throw new InvalidExpressionException("Unexpected tag");
}
}
reader.ReadEndElement();
}
insertCommand.CommandType = CommandType.StoredProcedure;
insertCommand.Parameters.AddWithValue("FirstName", first_name);
insertCommand.Parameters.AddWithValue("MiddleName", middle_name);
insertCommand.Parameters.AddWithValue("Surname", surname);
insertCommand.Parameters.AddWithValue("Gender", gender);
insertCommand.Parameters.AddWithValue("OccTitle", occ_title);
if (!((first_name == " " && surname == " " && middle_name == " " && gender == " " && occ_title == " ")))
{
insertCommand.ExecuteNonQuery();
}
}
while (reader.ReadToNextSibling("NameDetails")) ; // it will read next descendent of person
}
conn.Close();
}
}
}
}
随着调试,它从 if(reader.IsStartElement(“ NameDetails”))插入命令 请帮忙!
答案 0 :(得分:0)
检查以下代码将有助于工作:
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
}
}
XML文件:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>