gpt4 book ai didi

c# - 比较两个 xmlfiles,添加缺少的元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:06:32 27 4
gpt4 key购买 nike

这是一个棘手的问题。

我有一个文件,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 匹配,但我需要将所有文本值保留在 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

棘手的部分是嵌套,它可以是 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

那是因为列表的长度不一样,所以我需要将它添加到列表中......

最佳答案

据我了解,考虑到这两个文件具有相似的结构,您要做的是用另一个 xml 文件更新一个 xml 文件。如果是这样,您可以尝试使用 xpath 执行此操作,这类似于:

   private static void Main(string[] args)
{
try
{
XmlDocument xml1 = new XmlDocument();
xml1.Load(@"C:\testxml\MainFile.xml");
XPathNavigator nav = xml1.CreateNavigator();

//Create the langFile Navigator
XPathDocument xml2 = new XPathDocument(@"C:\testxml\LangFile.xml");
XPathNavigator nav2 = xml2.CreateNavigator();
//Select all text nodes.
var nodes = nav2.SelectDescendants(XPathNodeType.Text, true);
while (nodes.MoveNext())
{
//Update the MainFile with the values in the LangFile

var c = nav.SelectSingleNode(GetPath(nodes.Clone().Current));//(1*)
if(c != null)
{
c.MoveToFirstChild();
c.SetValue(nodes.Current.Value);
}
}
Console.WriteLine(xml1.InnerXml);
Console.ReadKey();
}
catch
{
}
}
private static string GetPath(XPathNavigator navigator)
{
string aux =string.Empty;
while (navigator.MoveToParent())
{
aux = navigator.Name + "/"+aux;
}
return "/" + (aux.EndsWith("/")?aux.Remove(aux.LastIndexOf('/')):aux);
}

有了这个你不需要知道你的文件嵌套了多少,但是如果xml在同一级别有多个同名节点,在(*1)注释的那一行,你应该管理它。我希望这会有所帮助。

关于c# - 比较两个 xmlfiles,添加缺少的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471268/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com