2017-02-01 1 views
0

Ich benutze C#.Xml Elemente überschreiben

Ich habe 2 XML-Dateien, das sind gleichen execpt spezifisches Element Wert aussehen:

Die Originaldatei:

<tasks> 
    <task id="1" > 
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> 
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 
     <CipherData> 
     <CipherValue>+bv8xdFfDzXai3rB1D+c2voJ/mRkuQHJfV34iWB2wyezR3wxG5UnLmznq4i2emIh4Z+8KukZEKJmM8=</CipherValue> 
     </CipherData> 
    </EncryptedData> 
    <AnotherElements/> 
    </task> 
    <task id="2" > 
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> 
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 
     <CipherData> 
     <CipherValue>+bv8xdFfDzXai3rB1D+c2voJ/mRkuQHJfV34iWB2wyezR3wxG5UnLmznq4i2emIh4Z+8KukZEKJmM8=</CipherValue> 
     </CipherData> 
    </EncryptedData> 
    <AnotherElements/> 
    </task> 
    ... 
<tasks> 

Und Backup-Datei:

<tasks> 
    <task id="1" > 
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> 
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 
     <CipherData> 
     <CipherValue>+asd+c2voJ/sdf+8KukZEKJmM8=</CipherValue> 
     </CipherData> 
    </EncryptedData> 
    <AnotherElements/> 
    </task> 
    <task id="2" > 
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> 
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 
     <CipherData> 
     <CipherValue>+asd+c2voJ/sdf+8KukZEKJmM8=</CipherValue> 
     </CipherData> 
    </EncryptedData> 
    <AnotherElements/> 
    </task> 
    ... 
<tasks> 

Im Fall eines Fehlers im Original Datei, weil das <EncryptedData> Element Ich möchte alle <EncryptedData> Element aus der Sicherungsdatei in der ursprünglichen Datei ersetzen.

Was ist der beste Weg, das zu tun?

Antwort

1

ein Linq

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 


namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string origXml = @"c:\temp\test1.xml"; 
     const string backupXml = @"c:\temp\test2.xml"; 
     static void Main(string[] args) 
     { 
      XDocument origDoc = XDocument.Load(origXml); 

      XDocument backupDoc = XDocument.Load(backupXml); 

      var groups = (from orig in origDoc.Descendants("task") 
          join backup in backupDoc.Descendants("task") on (int)orig.Attribute("id") equals (int)backup.Attribute("id") 
          select new { orig = orig, backup = backup }).ToList(); 

      foreach (var group in groups) 
      { 
       group.orig.Descendants().Where(x => x.Name.LocalName == "CipherValue").FirstOrDefault().Value = 
        (string)group.backup.Descendants().Where(x => x.Name.LocalName == "CipherValue").FirstOrDefault(); 
      } 
     } 
    } 

} 
1

Entfernen Sie die EncryptedData Elemente Join verwenden und dann die, die aus dem Backup hinzufügen. (In Anbetracht dessen, dass Ihr Namensraum der in Ihren Beispielen angegebene ist)

XDocument docOr = XDocument.Load(@"Path/To/Your/File/original.xml"); 
XDocument docBackup = XDocument.Load(@"Path/To/Your/File/backup.xml"); 

XNamespace ns = "http://www.w3.org/2001/04/xmlenc#"; 
foreach(XElement el in docOr.Root.Elements("task")) 
{ 
    el.Elements(ns+"EncryptedData").Remove(); 
    var NodesToAdd = docBackup 
      .Root 
      .Elements("task") 
      .First(x=>x.Attribute("id").Value==el.Attribute("id").Value) 
      .Elements(ns+"EncryptedData"); 
    foreach(XElement nta in NodesToAdd) 
    { 
     el.Add(nta); 
    } 
} 
docOr.Save(@"Path/To/Your/File/original.xml");