You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataTable仅存储单条XML飞机信息问题的解决方案咨询

问题分析与解决方案

你的问题核心很清晰:每次处理XML文件时,你都在重新创建一个全新的DataTable,而且调用Xmls()方法后没有保存返回的结果,导致之前处理的所有数据都直接丢失了,最终自然只能看到单条(甚至没有保留有效数据)。

具体问题点:

  • Xmls()方法每次被调用都会新建DataTable Xmls = new DataTable("AirCraftInfo");,完全没有复用之前的表数据
  • GatherXmlInfo()里调用Xmls(...)后,没有把返回的DataTable存储起来,相当于白创建了这个表
  • 代码里存在拼写错误:IspectionDate应该是InspectionDate(注意第二个字母是n),后续可能引发逻辑问题

修改步骤:

  1. 把DataTable提升为类成员变量(或者在循环前创建并传递),确保所有XML数据都添加到同一个表里
  2. 重构Xmls()方法,让它负责向已有的DataTable添加行,而不是每次新建表
  3. 修正拼写错误,并添加空值判断避免异常

修改后的完整代码示例:

首先,在你的类里定义一个全局的DataTable成员:

// 类成员变量,所有XML数据都会累积到这里
private DataTable _airCraftInfoTable;

然后在循环处理文件之前初始化这个表(只做一次):

// 初始化DataTable结构,仅执行一次
_airCraftInfoTable = new DataTable("AirCraftInfo");
_airCraftInfoTable.Columns.Add("AirCraft", typeof(string));
_airCraftInfoTable.Columns.Add("Inspection", typeof(string));
_airCraftInfoTable.Columns.Add("InspectionDate", typeof(string));
_airCraftInfoTable.Columns.Add("ImagePath", typeof(string));

接下来重构Xmls()方法(改个更直观的名字,比如AddAirCraftToTable):

public void AddAirCraftToTable(string aircraft, string inspection, string inspectionDate, string path)
{
    // 直接向已有的全局表添加新行
    DataRow newRow = _airCraftInfoTable.NewRow();
    newRow["AirCraft"] = aircraft;
    newRow["Inspection"] = inspection;
    newRow["InspectionDate"] = inspectionDate;
    newRow["ImagePath"] = path;
    _airCraftInfoTable.Rows.Add(newRow);
}

然后修改GatherXmlInfo()方法,调用重构后的方法并添加空值判断:

protected void GatherXmlInfo(string pathOfXml)
{
    doc.Load(pathOfXml);
    var airCraftNode = doc.DocumentElement.SelectSingleNode("/Document/Aircraft");
    var inspectionNode = doc.DocumentElement.SelectSingleNode("/Document/Inspection");
    var inspectionDateNode = doc.DocumentElement.SelectSingleNode("/Document/InspectionDate");

    // 先判断节点是否存在,避免XML格式异常时抛出空引用错误
    if (airCraftNode != null && inspectionNode != null && inspectionDateNode != null)
    {
        string aircraft = airCraftNode.InnerText;
        string inspection = inspectionNode.InnerText;
        string inspectionDate = inspectionDateNode.InnerText;
        // 将当前XML的数据添加到全局DataTable中
        AddAirCraftToTable(aircraft, inspection, inspectionDate, pathOfXml);
    }
}

最后是循环处理文件的代码,现在每次处理XML都会把数据累积到同一个DataTable里:

// 先初始化DataTable(这一步必须在循环前执行!)
_airCraftInfoTable = new DataTable("AirCraftInfo");
_airCraftInfoTable.Columns.Add("AirCraft", typeof(string));
_airCraftInfoTable.Columns.Add("Inspection", typeof(string));
_airCraftInfoTable.Columns.Add("InspectionDate", typeof(string));
_airCraftInfoTable.Columns.Add("ImagePath", typeof(string));

foreach (string fileName in fileEntries)
{
    string ext = Path.GetExtension(fileName);
    string filetmp = fileName;
    if (ext == ".tmp")
    {
        SendFileTAP(filetmp, PathTipologia);
    }
    else
    {
        string upperExt = ext.ToUpper();
        if (upperExt == ".PDF" || upperExt == ".XML")
        {
            CreateZips(filetmp);
            if (upperExt == ".XML")
            {
                GatherXmlInfo(filetmp);
            }
        }
    }
}

// 现在_airCraftInfoTable里就存储了所有XML文件的信息
// 你可以在这里用这个表做后续操作,比如绑定到GridView、导出Excel等

额外优化建议:

  • 给变量和方法起更清晰的名字(比如把列名AirCraftRow改成AirCraft,语义更明确)
  • 可以尝试用LINQ to XML替代SelectSingleNode,代码会更简洁易维护
  • 如果XML文件数量较多,可以考虑批量处理优化性能

内容的提问来源于stack exchange,提问作者Tiago Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 04:23:15