-3

我都想在 C# 中加载并比较条目,如果它们不存在,请添加它们,但不应添加某些异常。

我搜索了类似的问题,但找不到解决我的问题的方法。

在 C# 中修改 XML 现有内容

如何更改 XML 属性

//NEW
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <file-manager>
    <part-programs>
      <mount-points>
        <mount-point alias="USB (E:)">E:\</mount-point>
      </mount-points>
    </part-programs>
    <cad-parts>
      <mount-points>
        <mount-point alias="Part Programs">C:\Messer\Partprogram</mount-point>
      </mount-points>
    </cad-parts>
</configuration>

//OLD
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <file-manager>
    <part-programs>
      <mount-points>
        <mount-point alias="Floppy (A:)">A:\</mount-point>
        <mount-point alias="USB (E:)">E:\</mount-point>
        <mount-point alias="USB (F:)">F:\</mount-point>
        <mount-point alias="Part Programs">C:\Messer\Partprogram</mount-point>
        <mount-point alias="Programas">C:\Messer\ProgramasMaq2</mount-point>
      </mount-points>
    </part-programs>
    <cad-parts>
      <mount-points>
        <mount-point alias="Floppy (A:)">A:\</mount-point>
        <mount-point alias="USB (E:)">E:\</mount-point>
        <mount-point alias="USB (F:)">F:\</mount-point>
        <mount-point alias="Part Programs">C:\Messer\Partprogram</mount-point>
      </mount-points>
    </cad-parts>
</configuration>

//Should be added to NEW one

      <mount-points>
        <mount-point alias="USB (F:)">F:\</mount-point>
        <mount-point alias="Part Programs">C:\Messer\Partprogram</mount-point>
        <mount-point alias="Programas">C:\Messer\ProgramasMaq2</mount-point>
      </mount-points>

    <cad-parts>
      <mount-points>
        <mount-point alias="USB (E:)">E:\</mount-point>
        <mount-point alias="USB (F:)">F:\</mount-point>
      </mount-points>
    </cad-parts>


//Should not be added

      <mount-points>
        <mount-point alias="Floppy (A:)">A:\</mount-point>
      </mount-points>

    <cad-parts>
      <mount-points>
        <mount-point alias="Floppy (A:)">A:\</mount-point>
      </mount-points>
    </cad-parts>
4

1 回答 1

0

使用联接和左外部联接将起作用

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

namespace ConsoleApplication1
{
    class Program
    {
        const string NEW_FILENAME = @"c:\temp\test.xml";
        const string OLD_FILENAME = @"c:\temp\test1.xml";
        static void Main(string[] args)
        {
            XDocument newDoc = XDocument.Load(NEW_FILENAME);
            XDocument oldDoc = XDocument.Load(OLD_FILENAME);

            var query = (from oD in oldDoc.Descendants("file-manager").Elements()
                         join nD in newDoc.Descendants("file-manager").Elements() on oD.Name.LocalName equals nD.Name.LocalName
                         select new { name =  oD.Name.LocalName, oldDoc = oD, newDoc = nD })
                         .ToList();

            foreach (var part in query)
            {
                //left outer join
                var q = (from oldPart in part.oldDoc.Descendants("mount-point")
                         join newPart in part.newDoc.Descendants("mount-point") on (string)oldPart.Attribute("alias") equals (string)newPart.Attribute("alias") into ps
                         from newPart in ps.DefaultIfEmpty()
                         select new { newPart = newPart, oldPart = oldPart })
                        .ToList();

                XElement newMountPoint = part.newDoc.Descendants("mount-points").FirstOrDefault(); 

                foreach (var point in q)
                {
                    if (point.newPart == null)
                    {
                        newMountPoint.Add(point.oldPart);
                    }
                }
            }

        }
    }
}
于 2019-09-02T12:45:40.290 回答