C# 如何检查一个xml文件中的子节点是否等于另一个xml文件中的节点?
我需要经常生成一个C# 如何检查一个xml文件中的子节点是否等于另一个xml文件中的节点?,c#,xml,C#,Xml,我需要经常生成一个XML(a) XML(A)的结构是不可更改的。我只能更改我的XML(B)。我还可以将XML(B)存储在另一个文件/数据库/… 当生成XML(A)时。我需要检查XML(A)的一个子节点()是否在我的XML(B)中 当它在我的XML(B)中时,我想删除我的XML(A)中的整个父节点 当它不在我的XML(B)中时,我想在XML(B)中添加XML(A)的子节点 XML(A-Before) 阿尔法 123 贝塔 789 伽马射线 456 XML(B-Before) 123 4
XML(a)
XML(A)
的结构是不可更改的。我只能更改我的XML(B)
。我还可以将XML(B)
存储在另一个文件/数据库/…
当生成XML(A)
时。我需要检查XML(A)
的一个子节点(
)是否在我的XML(B)
中
- 当它在我的
中时,我想删除我的XML(B)
中的整个父节点XML(A)
- 当它不在我的
中时,我想在XML(B)
中添加XML(B)
的子节点XML(A)
XML(A-Before)
阿尔法
123
贝塔
789
伽马射线
456
XML(B-Before)
123
456
结果应该是这样的
XML(A-After)
贝塔
789
XML(B-After)
123
456
789
我的测试代码现在看起来像这样:
THX cptwonton,以XDocument为您提供建议
namespace ConsoleApp16
{
class Program
{
static void Main(string[] args)
{
XNodeEqualityComparer comparer = new XNodeEqualityComparer();
XDocument docA = XDocument.Load("XMLA.xml");
XDocument docB = XDocument.Load("XMLB.xml");
Dictionary<int, XNode> nodeDictionary = new Dictionary<int, XNode>();
SchleifeB();
SchleifeA();
docA.Save("XMLA.xml");
Console.ReadLine();
void SchleifeB()
{
foreach (XNode nodeB in docB.Elements("objektid").Elements("id"))
{
int hash = comparer.GetHashCode(nodeB);
nodeDictionary.Add(hash, nodeB);
Console.WriteLine("Eintrag in Dictonary hinzugefügt B");
Console.WriteLine(nodeB);
}
}
void SchleifeA()
{
foreach (XNode nodeA in docA.Elements("OPENCD").Elements("label").Elements("cd").Elements("id"))
{
int hashA = comparer.GetHashCode(nodeA);
if (nodeDictionary.ContainsKey(hashA))
{
Console.WriteLine("Duplikat gefunden A");
Console.WriteLine(nodeA);
nodeA.Parent.Parent.Remove();
//nodeA.Parent.Parent.Remove(); breaks the foreach. But I want it to loop trough each element to the end of my xmlA.
}
else
{
nodeDictionary.Add(hashA, nodeA);
Console.WriteLine("Eintrag in Dictonary hinzugefügt A");
Console.WriteLine(nodeA);
//Here I will add that nodeA gets pasted into XMLB
}
}
}
}
}
}
namespace控制台16
{
班级计划
{
静态void Main(字符串[]参数)
{
XNodeEqualityComparer comparer=新的XNodeEqualityComparer();
XDocument docA=XDocument.Load(“XMLA.xml”);
XDocument docB=XDocument.Load(“XMLB.xml”);
字典nodeDictionary=新字典();
schleifb();
Schleifa();
docA.Save(“XMLA.xml”);
Console.ReadLine();
void schleifb()
{
foreach(docB.Elements(“objektid”).Elements(“id”)中的XNode nodeB)
{
int hash=comparer.GetHashCode(nodeB);
添加(散列,nodeB);
Console.WriteLine(“在dictional hinzugefügt B中的Eintrag”);
控制台写入线(nodeB);
}
}
void schleifa()
{
foreach(docA.Elements(“OPENCD”).Elements(“标签”).Elements(“cd”).Elements(“id”)中的XNode节点)
{
int hashA=comparer.GetHashCode(nodeA);
if(nodeDictionary.ContainsKey(hashA))
{
控制台写入线(“Duplikat gefunden A”);
控制台写入线(nodeA);
nodeA.Parent.Parent.Remove();
//nodeA.Parent.Parent.Remove();中断foreach。但我希望它通过每个元素循环到xmlA的末尾。
}
其他的
{
添加(hashA,nodeA);
Console.WriteLine(“听写hinzugefügt A中的Eintrag”);
控制台写入线(nodeA);
//在这里,我将添加nodeA被粘贴到XMLB中
}
}
}
}
}
}
问题是
node.parent.parent.Remove()
中断foreach循环,在XML(A)
中只删除第一个找到的重复项 您可以采取的一种方法是:
- 使用XElement库的Load方法解析XML A和XML B文件。结果将得到两个字符串
- 执行比较(查看A中的id是否在B中,等等)。一种方法是使用正则表达式,但这可能有些过分。您可能只需要使用string.contains()方法就可以了
- 修改XML A和XML B的字符串表示形式
- 完成后,将字符串写回XMLs
如果我是你,我会使用XmlDocument库。这不是一个公共免费的代码编写服务。请向我们展示你迄今为止所做的一切你好,罗尔斯图法赫勒,这不是我的本意。谢谢你的建议!我会试试XML文档库。我读了一些关于XmlDocument的帖子。但我只能找到如何删除具有相同数据结构的重复项。thx cptwonton。我现在添加了我的测试代码。现在我的心都碎了。。。
<objektid>
<id>123</id>
<id>456</id>
</objektid>
<OPENCD>
<label>
<name>BETA</name>
<cd>
<id>789</id>
</cd>
</label>
</OPENCD>
<objektid>
<id>123</id>
<id>456</id>
<id>789</id>
</objektid>
namespace ConsoleApp16
{
class Program
{
static void Main(string[] args)
{
XNodeEqualityComparer comparer = new XNodeEqualityComparer();
XDocument docA = XDocument.Load("XMLA.xml");
XDocument docB = XDocument.Load("XMLB.xml");
Dictionary<int, XNode> nodeDictionary = new Dictionary<int, XNode>();
SchleifeB();
SchleifeA();
docA.Save("XMLA.xml");
Console.ReadLine();
void SchleifeB()
{
foreach (XNode nodeB in docB.Elements("objektid").Elements("id"))
{
int hash = comparer.GetHashCode(nodeB);
nodeDictionary.Add(hash, nodeB);
Console.WriteLine("Eintrag in Dictonary hinzugefügt B");
Console.WriteLine(nodeB);
}
}
void SchleifeA()
{
foreach (XNode nodeA in docA.Elements("OPENCD").Elements("label").Elements("cd").Elements("id"))
{
int hashA = comparer.GetHashCode(nodeA);
if (nodeDictionary.ContainsKey(hashA))
{
Console.WriteLine("Duplikat gefunden A");
Console.WriteLine(nodeA);
nodeA.Parent.Parent.Remove();
//nodeA.Parent.Parent.Remove(); breaks the foreach. But I want it to loop trough each element to the end of my xmlA.
}
else
{
nodeDictionary.Add(hashA, nodeA);
Console.WriteLine("Eintrag in Dictonary hinzugefügt A");
Console.WriteLine(nodeA);
//Here I will add that nodeA gets pasted into XMLB
}
}
}
}
}
}