linq to xml 如何将一个元素从一个 xml 移动到另一个 xml

发布于 2024-08-20 10:36:37 字数 2856 浏览 5 评论 0原文

我面临一个问题 我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-08-27 10:36:37

解决方案

在创建一个新节点以在 ProfileCatalog.xml 中移动时,

我将其替换

 pRes.Add(pluginToMove);

pRes.Add(new XElement(pns+ "ModuleInfo", new XAttribute("AssemblyFile", pluginToMove.Attribute("AssemblyFile").Value)));

Solution

while creating a new node to move in ProfileCatalog.xml

i replaced

 pRes.Add(pluginToMove);

with

pRes.Add(new XElement(pns+ "ModuleInfo", new XAttribute("AssemblyFile", pluginToMove.Attribute("AssemblyFile").Value)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文