首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查多个XML文件

检查多个XML文件
EN

Stack Overflow用户
提问于 2019-02-08 00:09:01
回答 2查看 191关注 0票数 1

我从我最初的帖子中改写了这一点:我有两个XML文件,每个文件都与给定的年份相关。例如,18/19和17/18。它们符合相同的结构,下面是其中一个文件的小样本。我想要的是,在C#中,比较这些文件中的所有记录,其中给定的名称、家庭名称、NI编号和出生日期相同,但学习者参考编号不同。我需要能够比较,然后只将这些记录推送到数据表中,这样我就可以将它们推送到电子表格中(我可以做的电子表格位)。我目前有以下作为起点的块,但仍然非常卡住。

首先,我按下了导入按钮:

代码语言:javascript
运行
复制
        private void Btn_Import_Click(object sender, RoutedEventArgs e)
    {


        ILRChecks.ILRReport.CrossYear();}

然后查看最终将文件推送到我的位置的Class:

代码语言:javascript
运行
复制
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ILRValidation;
using InfExcelExtension;

namespace ILRChecks
{
internal static partial class ILRReport
{
    internal static void CrossYear()
    {
        DataSet ds_CrossYearChecks = 
ILRValidation.Validation.CrossYearChecks(Global.fileNames);

        string output = Path.Combine(Global.foldername, "ULIN_Issues" + 
".xlsx");


        ds_CrossYearChecks.ToWorkBook(output);




    }
}
}

这就是我坚持的部分,这是找出差异的结果:

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ILRValidation
{
public static partial class Validation
{
    public static DataSet CrossYearChecks(DataSet ds_CrossYearChecks)
    {
        return CrossYearChecks(ds_CrossYearChecks);
    }

    public static DataSet CrossYearChecks(string[] xmlPath)
    {

        DataSet ds_xmlCrossYear = new DataSet();


        return CrossYearChecks(ds_xmlCrossYear);
    }
}
}

XML:

代码语言:javascript
运行
复制
<Learner>
<LearnRefNumber></LearnRefNumber>
<ULN></ULN>
<FamilyName></FamilyName>
<GivenNames></GivenNames>
<DateOfBirth></DateOfBirth>
<Ethnicity></Ethnicity>
<Sex></Sex>
<LLDDHealthProb></LLDDHealthProb>
<NINumber></NINumber>
<PriorAttain></PriorAttain>
<MathGrade></MathGrade>
<EngGrade></EngGrade>
<PostcodePrior></PostcodePrior>
<Postcode></Postcode>
<AddLine1></AddLine1>
<AddLine3></AddLine3>
<Email></Email>
EN

Stack Overflow用户

发布于 2019-02-08 01:09:22

在我看来,您在从xml中提取所需的值时遇到了问题,对吗?

正如其他人在评论中提到的那样,如果不知道xml的布局,就不可能为您的情况提供具体的示例。如果您将问题编辑为包含xml的示例,我们可以提供更多帮助。

下面是一些关于如何从xml中提取值的通用示例:

代码语言:javascript
运行
复制
private static bool CheckXmlDocument(string xmlPathCheck)
{
    // if you have multiple files from which you need to extract values, pass in an array or List<string> and loop over it, fetching the values

    // XmlDocument will allow you to edit the document as well as read it
    // there's another option to use XPathDocument and XPathNavigator but it's read-only
    var doc = new XmlDocument();

    // this can throw various exceptions so might want to add some handling
    doc.Load(xmlPathCheck);

    // getting the elements, you have some options depending on the layout of the document
    // if the nodes you want are identified by 'id' use this:
    var nameElement = doc.GetElementById("name");

    // if the nodes you want are identified by 'tag', use this:
    var nameNodeList = doc.GetElementsByTagName("name");

    // if you know the xpath to the specific node you want, use this:
    var selectNameNode = doc.SelectSingleNode("the/xpath/to/the/node");

    // if there are several nodes that have the same xpaths, use this:
    var selectNameList = doc.SelectNodes("the/xpath/that/may/match/many/nodes");

    // getting the value depends on the if you have an XmlNode, XmlElement or XmlNodeList
    // if you have a single XmlElement or XmlNode you can get the value one of these ways depending on the layout of your document:
    var name = nameElement.InnerText;
    name = nameElement.InnerXml;

    // if you have an XmlNodeList, you'll have to iterate through the nodes to find the one you want, like this:
    foreach (var node in nameNodeList)
    {
        // here use some condition that will determine if its the element/node you want or not (depends on your xml layout)
        if (node is XmlNode n)
        {
            name = n.InnerText;
        }
    }

    // do that for all the values you want to compare, then compare them
    return CheckValues(/*the values to compare*/);
}

XmlDocument

XmlNode

XmlElement

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54577516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档