linq to xml 如何将一个元素从一个 xml 移动到另一个 xml
我面临一个问题 我有两个 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。
请给我一个解决方案。
i am facing one problem
i have two xml files.
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>
i wants to move PluginXXX.dll from Plugins.xml to ProfileCatalog.xml.
the code i used is as
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();
}
NOTE there is xmlns difference between both files.
when i move the node from Plugins.xml to ProfileCatalog.xml it adds the element as
<ModuleInfo AssemblyFile="PluginXXX.dll" xmlns="http://schemas.microsoft.com/pag/cab-profile" />
I want to remove this xmlns before adding to this file.
kindly give me a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案
在创建一个新节点以在 ProfileCatalog.xml 中移动时,
我将其替换
为
Solution
while creating a new node to move in ProfileCatalog.xml
i replaced
with