我正面临一个问题 我有两个xml文件。
Plugins.xml
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">
<Modules>
<ModuleInfo AssemblyFile="PluginXXX.dll" />
</Modules>
</SolutionProfile>
ProfileCatalog.xml
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile/2.0">
<Section Name="Services">
<Modules>
<ModuleInfo AssemblyFile="Module.dll" />
</Modules>
</Section>
<Section Name="Apps">
<Dependencies>
<Dependency Name="Services" />
</Dependencies>
<Modules>
<ModuleInfo AssemblyFile="PluginABC.dll" >
</Modules>
</Section>
</SolutionProfile>
我想将PluginXXX.dll从Plugins.xml移到ProfileCatalog.xml。
我使用的代码是
public static void PluginLoaded(string pluginName)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
XDocument pluginDoc = XDocument.Load("Plugins.xml");
XDocument profileCatalogDoc = XDocument.Load("ProfileCatalog.xml");
XmlWriter pluginDocXmlWriter = XmlWriter.Create("Plugins.xml", settings);
XmlWriter profileCatalogDocXmlWriter = XmlWriter.Create("ProfileCatalog.xml", settings);
XNamespace ns = "http://schemas.microsoft.com/pag/cab-profile";
var res = from plugin in pluginDoc.Descendants(ns + "ModuleInfo")
where plugin.Attribute("AssemblyFile").Value == pluginName
select plugin;
// save the plugin to remove
XElement pluginToMove = res.FirstOrDefault();
//remove from xml
res.FirstOrDefault().Remove();
// save xml back
pluginDoc.Save(pluginDocXmlWriter);
XNamespace pns = "http://schemas.microsoft.com/pag/cab-profile/2.0";
var pRes = ((from pcPlugin in profileCatalogDoc.Descendants(pns + "Modules")
select pcPlugin).Skip(1).Take(1)).FirstOrDefault();
pRes.Add(pluginToMove);
profileCatalogDoc.Save(profileCatalogDocXmlWriter);
//TODO : move the plugin name from Plugins.xml to ProfileCatalog.xml
profileCatalogDocXmlWriter.Close();
pluginDocXmlWriter.Close();
}
注意两个文件之间存在xmlns差异。 当我将节点从Plugins.xml移动到ProfileCatalog.xml时,它将元素添加为
<ModuleInfo AssemblyFile="PluginXXX.dll" xmlns="http://schemas.microsoft.com/pag/cab-profile" />
我想在添加到此文件之前删除此xmlns。
请给我一个解决方案。
答案 0 :(得分:0)
<强>解决方案强>
创建一个新节点以在ProfileCatalog.xml中移动
我换了
pRes.Add(pluginToMove);
与
pRes.Add(new XElement(pns+ "ModuleInfo", new XAttribute("AssemblyFile", pluginToMove.Attribute("AssemblyFile").Value)));