2010-02-02 2 views
1

Je suis confronté à un problème J'ai deux fichiers xml.linq to xml comment déplacer un élément d'un xml à un autre

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 veut passer PluginXXX.dll de Plugins.xml à ProfileCatalog.xml.

le code i utilisé est comme

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 il y a xmlns différence entre les deux fichiers. lorsque je déplace le noeud de Plugins.xml à ProfileCatalog.xml il ajoute l'élément comme

<ModuleInfo AssemblyFile="PluginXXX.dll" xmlns="http://schemas.microsoft.com/pag/cab-profile" /> 

Je veux enlever cette xmlns avant d'ajouter à ce fichier.

veuillez me donner une solution.

+0

Je résolu ce problème :) – Mohsan

+0

Il serait OK (encouragé même) pour vous d'afficher votre solution comme réponse à votre propre question. – CoderDennis

+0

bien sûr laissez-moi écrire – Mohsan

Répondre

0

Solution

tout en créant un nouveau nœud de se déplacer dans ProfileCatalog.xml

i remplacé

pRes.Add(pluginToMove); 

avec

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