首页 > 解决方案 > 如何在 xml 文件中查找特定节点?

问题描述

我有一个使用控制台 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")) 到插入命令 请帮助!

标签: c#

解决方案


检查下面的代码它将有助于工作:

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>

推荐阅读