DataTable 插入仅添加值行

DataTable inserting only adds on row of value

我有这个数据表,我在其中存储 airCraft 信息,但是当我查看数据表时,它只存储了 xml 文件中的一个条目,但我所有的 xml 文件都在不断读取并且 运行 槽,我该怎么做才能让dataTable获取所有信息。

public DataTable Xmls(string aircraft, string Inspection, string IspectionDate, string path)
    {
        DataTable Xmls = new DataTable("AirCraftInfo");
        DataRow AirCraftRow = Xmls.NewRow();
        DataRow InspectionRow = Xmls.NewRow();
        DataRow InspectionDateRow = Xmls.NewRow();
        DataRow ImagePathRow = Xmls.NewRow();

        Xmls.Columns.Add("AirCraftRow", typeof(string));
        Xmls.Columns.Add("InspectionRow", typeof(string));
        Xmls.Columns.Add("InspectionDateRow", typeof(string));
        Xmls.Columns.Add("ImagePathRow", typeof(string));

        Xmls.Rows.Add(aircraft, Inspection, IspectionDate, path);

        return Xmls;
    }


        

       protected void GatherXmlInfo(string PathOfXml)
    {
        
        
        doc.Load(PathOfXml);

        AirCraft = doc.DocumentElement.SelectSingleNode("/Document/Aircraft");
        Inspection = doc.DocumentElement.SelectSingleNode("/Document/Inspection");
        InspectionDate = doc.DocumentElement.SelectSingleNode("/Document/InspectionDate");


        aircraft = AirCraft.InnerText;

        inspection = Inspection.InnerText;

        inspectiondate = InspectionDate.InnerText;

        Xmls(aircraft, inspection, inspectiondate, PathOfXml);

    }

foreach (string fileName in fileEntries)
            {

                ext = Path.GetExtension(fileName);

                filetmp = fileName;

                if (ext == ".tmp")
                {
                    SendFileTAP(filetmp, PathTipologia);

                }
                else if (ext != ".tmp")
                {
                    if (ext.ToUpper() == ".PDF" || ext.ToUpper() == ".XML")
                    {
                        CreateZips(filetmp);

                        if(ext.ToUpper() == ".XML")
                        {
                            GatherXmlInfo(filetmp);
                        }

                    }
                }
            }

提前感谢您的帮助。

您不应该创建一个方法来创建和填充 DataTable,您正在覆盖其中的数据(仅存储其中的最后一行)。

public DataTable CreateTable()
{
    DataTable xmls = new DataTable("AirCraftInfo");

    xmls.Columns.Add("AirCraft", typeof(string));
    xmls.Columns.Add("Inspection", typeof(string));
    xmls.Columns.Add("InspectionDate", typeof(string));
    xmls.Columns.Add("ImagePath", typeof(string));

    return Xmls;
}

public void AddDataToTable(DataTable xmls, string aircraft, string inspection, string ispectionDate, string path)
{
    DataRow rowToAdd = xmls.NewRow();

    rowToAdd["AirCraft"] = aircraft;
    rowToAdd["Inspection"] = inspection;
    rowToAdd["InspectionDate"] = ispectionDate;
    rowToAdd["ImagePath"] = path;

    xmls.Rows.Add(rowToAdd);
}

并这样称呼它:

DataTable xmls = CreateTable();

foreach (string fileName in fileEntries)
{
    string aircraft = doc.DocumentElement.SelectSingleNode("/Document/Aircraft");
    string inspection = doc.DocumentElement.SelectSingleNode("/Document/Inspection");
    string inspectionDate = doc.DocumentElement.SelectSingleNode("/Document/InspectionDate");
    string path = "";

    AddDataToTable(xmls, aircraft, inspection, inspectionDate, path);
}