这里有一个棘手的问题。
我有一个文件 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>
我想更新我的 LangFile.XML 以使其与我的 MainFile.XML 匹配,但我需要将所有 Text 值保留在 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>
我已经看过这个答案,但我需要更新文件并保留值... 逐行比较两个文本文件
棘手的部分是嵌套,它可以是 1 级到 X 级深的任何东西......
我的问题是,当我在树中深入时,我不知道如何逐行比较行,我尝试过这样的事情,但我卡住了......我不知道如何将特定的后代添加到新列表中。
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
那是因为列表的长度不同,这就是为什么我需要将它添加到列表中......