여기 까다로운 질문이 있습니다. 두 개의 xml 파일 비교 누락 된 요소 추가
나는 다음과 같습니다 하나 개의 파일, MainFile.XML 가지고 :<xml>
<header>
<some></some>
<addThis></addThis>
</header>
<footer></footer>
<this>
<is>
<deep>
<like></like>
</deep>
</is>
</this>
<test></test>
<page></page>
<addThis></addThis>
을 그리고 내 다른 파일 LangFile.XML은 다음과 같습니다.
<xml>
<header>
<some>English file</some>
</header>
<footer>Footer</footer>
<this>
<is>
<deep>
<like>Hey</like>
</deep>
</is>
</this>
<test>Something</test>
</xml>
나는 내 MainFile.XML 일치하도록 내 LangFile.XML을 업데이트 할하지만 난 LangFile의 모든 텍스트 값을 유지해야합니다.
나는 LangFile는 업데이트 후 다음과 같이 할 : 예상 출력
<xml>
<header>
<some>English file</some>
<addThis></addThis>
</header>
<footer>Footer</footer>
<this>
<is>
<deep>
<like>Hey</like>
</deep>
</is>
</this>
<test>Something</test>
<page></page>
<addThis></addThis>
</xml>
나는이 대답을 검토 한하지만 파일을 업데이트하고 값을 유지할 필요가 ... Compare two text files line by line
까다로운 부분은 중첩, 그것은
내 문제는 내가 거라고 있다는 것입니다 ... 깊은 X 레벨 1 개 수준까지 사이에 무엇이든 될 수있다 ont 트리에서 더 깊게 진행될 때 행 단위로 행을 비교하는 방법을 알고 있습니다. 그러나 이와 같은 시도를 했었지만 im이 붙어 있습니다 ... 새 목록에 특정 자손을 추가하는 방법을 모릅니다.
String directory = @"C:\Utv\XmlTest";
var mainFile = XDocument.Load(Path.Combine(directory, "MainFile.XML"));
var langFile = XDocument.Load(Path.Combine(directory, "LangFile.XML"));
//Get all descendant nodes
var mainFileDesc = mainFile.Root.Descendants().ToList();
var langFileDesc = langFile.Root.Descendants().ToList();
//Loop through the mainfile
for(var i = 0; i < mainFileDesc.Count(); i++)
{
var mainRow = mainFileDesc[i];
var langRow = langFileDesc[i];
//Compare the rows descendants, if not the same, add the mainRow to the langRow
if(mainRow.Descendants().Count() != langRow.Descendants().Count())
{
//Here I want to check if the mainRow != the langRow
//if not, add the mainRow to the langFile list
if(mainRow != langRow)
{
langFileDesc.Insert(i, mainRow);
}
}
}
임 이제 다음 오류가 : 나는 목록에 추가해야하는 이유
목록이 동일한 길이를 가지고 있지 않기 때문이다var langRow = langFileDesc[i];
Message Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
, 그건 ...
몇 개의 아이템이있을 수 있습니까? 얼마나 중첩 되니? 모든 ID 값 은요? 2 개의 텍스트 값이 다른 경우 어떻게됩니까? –
주 파일에는 절대로 텍스트 값이 없습니다. 중첩은 융통성이 있으며, 1 -1000 레벨 사이의 어떤 것도 될 수 있습니다 ... – JOSEFtw
1000 레벨? XML을 사용해서는 안되며 여기서 재귀적인 해결책을 찾고있을 것입니다. –