C #을 사용하고 있습니다.
동일한 execpt 특정 요소 값으로 보이는 2 개의 xml 파일이 있습니다.
원본 파일 :
<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>
그리고 백업 파일 :
<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>
원본 파일에 오류가있는 경우 원본 파일 의 백업 파일에서 <EncryptedData>
모든 <EncryptedData>
요소 를 대체하고 싶은 요소 이기 때문 입니다.
그렇게하는 가장 좋은 방법은 무엇입니까?
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();
}
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다