【发布时间】:2013-09-26 02:56:30
【问题描述】:
我有两个 XML 文件(*.resx 文件),我试图将它们合并到一个删除重复文件中,但我无法这样做。我尝试了以下方法但没有成功:
var resource1 = XDocument.Load("C:\\Resources.resx");
var resource2 = XDocument.Load("C:\\Resources2.resx");
// This results in a file with all the nodes from the second file included inside
// the root element of the first file to form a properly formatted, concatenated file.
resource1.Descendants().FirstOrDefault().Add(resource2.Descendants().FirstOrDefault().Nodes());
var nodeContent = new List<string>();
foreach (XElement node in resource1.Root.Elements())
{
if (nodeContent.Contains(node.ToString()))
resource1.Remove();
else
nodeContent.Add(node.ToString());
}
resource1.Save("C:\\FinalResources.resx");
在删除语句中,我得到一个 InvalidOperationException - “父级丢失。”:
我做错了吗?
【问题讨论】:
-
您应该定义在您的上下文中什么是“重复”。你如何比较两个 xml 条目?通过
name属性?按标签名称?按内容? -
如果还有人在寻找这个问题的答案,那么请看这里。 stackoverflow.com/questions/982597/…
标签: c# xml linq-to-xml