gpt4 book ai didi

c# - 使用 C# 合并新旧 XML 文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:13 26 4
gpt4 key购买 nike

在过去的几周里,我一直在尝试创建一个 C# 应用程序,将 2 个 XML 文件合并到一个新的 XML 文件中。当您知道 XML 文件的布局时,这本身并不难,但在这种情况下,我不知道文件的布局。它需要是 100% 动态的,而不需要在代码中命名标签。

应用程序需要按以下方式将 2 个文件合并在一起。旧文件中的标签是领先的,这些标签的值需要在最终结果中,但是它不能有任何重复。但是如果新的 XML 文件中有新的标签,则需要将其添加到最终 XML 文件的正确位置。

为了向您展示我的意思,我创建了以下示例:

XML 文件 1:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="BaseAddress" value="http://HOSTNAME:8820/" />
<add key="OIPServerUrl" value="http://HOSTNAME:4512/" />
<add key="WFAddress" value="http://HOSTNAME:8804" />
<add key="WFUser" value="USER" />
<add key="WFPassword" value="" />
<add key="SiteRoot" value="somepath" />
<add key="AlternateImages" value="somepath" />
<add key="DataFolder" value="somepath2222" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<!-- Non User configurable part -->
<system.serviceModel>
<services>
<service name="S4M.Gen.Services.OIPData">
<endpoint binding="webHttpBinding" contract="S4M.Gen.Model.IOIPData" behaviorConfiguration="WebHttpJsonBehavior">
</endpoint>
</service>
<service name="S4M.OIP.Factory.Services.CSDMService">
<endpoint binding="webHttpBinding" contract="Base.Model.ServiceContracts.IDMService" behaviorConfiguration="WebHttpJsonBehavior">
</endpoint>
</service>
<service name="S4M.WebServerService.StaticFileService">
<endpoint binding="webHttpBinding" behaviorConfiguration="web" contract="S4M.WebServerService.IStaticFileService">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding contentTypeMapper="Base.Service.ServiceContracts.RawMapper, Base.Service" />
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="webHttpJson" type="Base.Service.ServiceContracts.WebHttpJsonElement, Base.Service" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
<behavior name="WebHttpJsonBehavior">
<webHttpJson />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>

XML 文件 2:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="BaseAddress" value="http://HOSTNAME:8820/" />
<add key="OIPServerUrl" value="http://HOSTNAME:4512/" />
<add key="WFAddress" value="http://HOSTNAME:8804" />
<add key="WFUser" value="USER" />
<add key="WFPassword" value="" />
<add key="SiteRoot" value="somepath" />
<add key="AlternateImages" value="somepath" />
<add key="DataFolder" value="somepath" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
<add key="RedirectCode" value="TemporaryRedirect"/> <!-- NEW tag -->
</appSettings>
<!-- Non User configurable part -->
<system.serviceModel>
<services>
<service name="S4M.Gen.Services.OIPData">
<endpoint binding="webHttpBinding" contract="S4M.Gen.Model.IOIPData" behaviorConfiguration="WebHttpJsonBehavior">
</endpoint>
</service>
<service name="S4M.OIP.Factory.Services.CSDMService">
<endpoint binding="webHttpBinding" contract="Base.Model.ServiceContracts.IDMService" behaviorConfiguration="WebHttpJsonBehavior">
</endpoint>
</service>
<service name="S4M.WebServerService.StaticFileService">
<endpoint binding="webHttpBinding" behaviorConfiguration="web" contract="S4M.WebServerService.IStaticFileService">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding contentTypeMapper="Base.Service.ServiceContracts.RawMapper, Base.Service" />
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="webHttpJson" type="Base.Service.ServiceContracts.WebHttpJsonElement, Base.Service" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
<behavior name="WebHttpJsonBehavior">
<webHttpJson />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>

最终结果:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="BaseAddress" value="http://HOSTNAME:8820/" />
<add key="OIPServerUrl" value="http://HOSTNAME:4512/" />
<add key="WFAddress" value="http://HOSTNAME:8804" />
<add key="WFUser" value="USER" />
<add key="WFPassword" value="" />
<add key="SiteRoot" value="somepath" />
<add key="AlternateImages" value="somepath" />
<add key="DataFolder" value="somepath2222" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
<add key="RedirectCode" value="TemporaryRedirect"/> <!-- NEW tag -->
</appSettings>
<!-- Non User configurable part -->
<system.serviceModel>
<services>
<service name="S4M.Gen.Services.OIPData">
<endpoint binding="webHttpBinding" contract="S4M.Gen.Model.IOIPData" behaviorConfiguration="WebHttpJsonBehavior">
</endpoint>
</service>
<service name="S4M.OIP.Factory.Services.CSDMService">
<endpoint binding="webHttpBinding" contract="Base.Model.ServiceContracts.IDMService" behaviorConfiguration="WebHttpJsonBehavior">
</endpoint>
</service>
<service name="S4M.WebServerService.StaticFileService">
<endpoint binding="webHttpBinding" behaviorConfiguration="web" contract="S4M.WebServerService.IStaticFileService">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding contentTypeMapper="Base.Service.ServiceContracts.RawMapper, Base.Service" />
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="webHttpJson" type="Base.Service.ServiceContracts.WebHttpJsonElement, Base.Service" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
<behavior name="WebHttpJsonBehavior">
<webHttpJson />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>

我目前的代码如下:

 XDocument doc1 = XDocument.Load(@"C:\Users\Jack\Desktop\1.config");
XDocument doc2 = XDocument.Load(@"C:\Users\Jack\Desktop\2.config");

XDocument newdoc = new XDocument();


var query1 = from el
in doc2.Elements().Except(doc1.Elements())
select el;
var query2 = from el
in doc1.Elements().Except(doc2.Elements())
select el;

var query = query2.Except(query1);

foreach (var item in query)
{
newdoc.Add(item);

Console.WriteLine(item);
}
newdoc.Save(@"C:\Users\Jack\Desktop\new.config");
Console.ReadLine();

我已经研究了很长时间了,但我似乎无法弄清楚。有人可以帮助我吗?

最佳答案

你可以这样试试:

XDocument doc1 = XDocument.Load(@"C:\Users\Jack\Desktop\1.config");
XDocument doc2 = XDocument.Load(@"C:\Users\Jack\Desktop\2.config");
var query1 =
doc2.Root.Elements().Where(
o => !doc1.Root.Elements().Select(p => p.Name.LocalName).Contains(o.Name.LocalName));
foreach (var item in query1)
{
doc1.Root.Add(item);
}
doc1.Save(@"C:\Users\Jack\Desktop\new.config");

关于c# - 使用 C# 合并新旧 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21699120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com