【问题标题】:How do I connect with other xml file xml file如何连接其他 xml 文件 xml 文件
【发布时间】:2015-05-05 20:16:12
【问题描述】:

我有两个使用 XML 数据处理文件的 XML 文件如何将它们相互连接?

例子:

members.xml

<?xml version="1.0" encoding="UTF-8"?>
<Data>
<member id="1" username="selahattin" name="Selahattin" surname="Yüksel"/>
<member id="2" username="test" name="John" surname="Bravo"/>
</Data>

cmets.xml

 <?xml version="1.0" encoding="UTF-8"?>
<Data>
<member id="1" username="selahattin" comment="Hi, stackoverflow"/>
<member id="2" username="test" comment="Hello world!"/>
</Data>

在数据的用户名字段中使用两个 XML 文件你想怎么取这个?

用户名==用户名

【问题讨论】:

  • 我不明白你的意思。您是什么意思,“将它们相互连接”?另外,您使用的是哪个版本的 .NET?
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 我使用的是 4.0.我将通过连接XML文件中的两个字段例如用户名SQL内连接来列出数据。
  • 请显示您的预期结果。并请说明您是如何尝试解决问题的。我们不会为你做你的工作。
  • 对不起,我不会说英语。你能举例说明我如何做到这一点吗?

标签: c# xml


【解决方案1】:

这是一种方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME1 = @"c:\temp\test.xml";
        const string FILENAME2 = @"c:\temp\test2.xml";
        static void Main(string[] args)
        {
            DataSet ds1 = new DataSet();
            ds1.ReadXml(FILENAME1);
            DataSet ds2 = new DataSet();
            ds2.ReadXml(FILENAME2);

            var results = from dataRows1 in ds1.Tables["member"].AsEnumerable()
                                     join dataRows2 in ds2.Tables["member"].AsEnumerable()
                                     on dataRows1.Field<string>("id") equals dataRows2.Field<string>("id") into ps
                                     select dataRows1;
            DataTable combinedTable = results.CopyToDataTable();

        }
    }
}​

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
相关资源
最近更新 更多