C# 如何在xml文件中查找特定节点?
我有一个4GB的xml文件,我用控制台c#读取它。现在我想把一些数据从它插入数据库。节点“NeimeDebug”及其寻找的孩子位于文件中间的某个地方。我编写的代码找不到该节点。在本例中,正在抛出错误意外节点 代码是:C# 如何在xml文件中查找特定节点?,c#,C#,我有一个4GB的xml文件,我用控制台c#读取它。现在我想把一些数据从它插入数据库。节点“NeimeDebug”及其寻找的孩子位于文件中间的某个地方。我编写的代码找不到该节点。在本例中,正在抛出错误意外节点 代码是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using
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();
}
}
}
}
随着调试的进行,它将从
如果将(reader.IsStartElement(“NameDetails”))添加到insert命令
请帮忙 检查以下代码,这将有助于工作:
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);
}
}
}
使用系统;
使用System.IO;
使用System.Xml;
公共类样本
{
公共静态void Main()
{
//创建XmlDocument。
XmlDocument doc=新的XmlDocument();
doc.Load(“books.xml”);
//显示所有书名。
XmlNodeList elemList=doc.GetElementsByTagName(“标题”);
for(int i=0;i
XML文件:
富兰克林自传
本杰明
富兰克林
8.99
自信的人
赫尔曼
梅尔维尔
11.99
高尔吉亚
柏拉图
9.99
错误是。。。。?请编辑您的问题并粘贴完整的stacktrace好吗?哪一行代码引发了哪一个错误?还有一件事:如果您需要从xml文件中插入一些数据,我建议您首先解析文件并在打开数据库连接之前获取所需的数据。它不会引发任何错误,只是没有插入数据。从此行中,如果(reader.IsStartElement(“NameDetails”){插入命令末尾的代码无法加载xml文件,因为它太大了。真的吗?我认为这不应该是一个问题,请再试一次。我已经试过了,它从MemoryException中获得了此错误,具体到doc.load(然后抛出没有内存的错误
<?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>