在过去的几周里,我一直在尝试创建一个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();
我已经在这方面工作了很长一段时间,但我似乎无法弄明白。有人能帮助我吗?
答案 0 :(得分:2)
您可以尝试这种方式:
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");