-1

我正在尝试加入两个 xml 文件 doc1 和 doc2,我试图做的加入是在 Modified 属性上,但查询没有返回任何内容,我不知道为什么。后代在两个文件中都是版本,并且在两者中都修改了属性,任何人都可以看到为什么我的加入不起作用?我对 c# 并不陌生,但对 xml 搜索 web 找到的示例很陌生,但仍然无法让它们在我的 xml 文件上工作。

<Versions xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<Version Status="Closed" Modified="2013-04-15T06:50:13Z" />
<Version Status="Active" Modified="2012-08-21T08:47:52Z" />
<Version Status="New" Modified="2012-08-21T08:43:52Z" />
</Versions>



  <Versions xmlns="http://schemas.microsoft.com/sharepoint/soap/">
  <Version Edited="DM" Modified="2013-04-15T06:50:13Z" />
  <Version Edited="KM" Modified="2012-08-21T08:47:52Z" />
  <Version Edited="CM" Modified="2012-08-21T08:43:52Z" />
  </Versions>

         XElement  doc1 = XElement.Load(@".....\Documents\doc1.xml");
         XElement  doc2 = XElement.Load(@"......\Documents\doc2.XML");
         var query = from f in doc1.Descendants("Version")
                     join r in doc2.Descendants("Version")
                     on
                        (string)f.Attribute("Modified")
                     equals
                        (string)r.Element("Modified")

                     select new
                     {
                         moddate = (string)f.Attribute("Modified")
                     };
         foreach (var k in query)
         {
             Console.WriteLine(k);
         }
4

1 回答 1

0

您需要考虑命名空间:

    XDocument doc1 = XDocument.Load("../../XMLFile1.xml");
    XDocument doc2 = XDocument.Load("../../XMLFile2.xml");

    XNamespace df = "http://schemas.microsoft.com/sharepoint/soap/";

    var joinedData =
        from version in doc1.Root.Elements(df + "Version")
        join version2 in doc2.Root.Elements(df + "Version")
        on (DateTime)version.Attribute("Modified") equals (DateTime)version2.Attribute("Modified")
        select new {
            modDate = (DateTime)version.Attribute("Modified"),
            status = (string)version.Attribute("Status"),
            edited = (string)version2.Attribute("Edited")
        };

    foreach (var data in joinedData)
    {
        Console.WriteLine("{0} edited at {1} with status {2}.", data.edited, data.modDate, data.status);
    }

这样,对于您的两个输入样本,我都会得到结果

DM edited at 15.04.2013 06:50:13 with status Closed.
KM edited at 21.08.2012 08:47:52 with status Active.
CM edited at 21.08.2012 08:43:52 with status New.
于 2013-06-28T12:18:34.993 回答