Xml 使用 C# 合并
Xml merge using C#
xml1:
<value>
<Id>1</id>
<name>AAA</name>
</value>
<value>
<Id>2</id>
<name>bbb</name>
</value>
<value>
<Id>3</id>
<name>ccc</name>
</value>
xml2:
<value>
<Id>1</id>
<Company>abc</Company>
<address>ASD</address>
</value>
result :
<value>
<Id>1</id>
<name>AAA</name>
<Company>abc</Company>
<address>ASD</address>
</value>
<value>
<Id>2</id>
<name>bbb</name>
</value>
<value>
<Id>3</id>
<name>ccc</name>
</value>
if id of 1st xml = 2nd xml then merge the 2nd xml record with 1st xml value record.
在我的代码中,我没有检查 id 值,但我只是试图将第二条 xml 记录与 root 和 1xml 记录合并。如果你能帮助我,那就太好了。我得到错误有要插入的节点来自不同的文档上下文。
XmlNode x1 = doc1.SelectSingleNode("/OutLooksync/value");
foreach (XmlNode node in x1.SelectNodes("/OutLooksync/value"))
{
x1.AppendChild(doc2.ImportNode(node, true));
}
XmlNodeList x1 = doc1.SelectSingleNodes("/OutLooksync/value");
foreach (XmlNode node in x1)
{
doc2.appendChild(node);
}
通过使用 XmlDocument
,
- 您可以在
xml1
中的 values 和 xml2
[=33 中的 values 中循环=]
- 检查
id
是否等于,并从 xml2
导入 childNodes
1 - Xml 用于测试:
string xml1 = @"
<OutLooksync>
<value>
<id>1</id>
<name>AAA</name>
</value>
<value>
<id>2</id>
<name>bbb</name>
</value>
<value>
<id>3</id>
<name>ccc</name>
</value>
</OutLooksync>";
string xml2 = @"
<OutLooksync>
<value>
<id>1</id>
<Company>abc</Company>
<address>ASD</address>
</value>
</OutLooksync>";
2 - 导入节点的代码
XmlDocument xmlDocument1 = new XmlDocument();
xmlDocument1.LoadXml(xml1);
XmlDocument xmlDocument2 = new XmlDocument();
xmlDocument2.LoadXml(xml2);
XmlNodeList values1 = xmlDocument1.SelectNodes("/OutLooksync/value");
XmlNodeList values2 = xmlDocument2.SelectNodes("/OutLooksync/value");
foreach(XmlNode value1 in values1)
{
foreach(XmlNode value2 in values2)
{
if(value1.SelectSingleNode("./id").InnerText != value2.SelectSingleNode("./id")?.InnerText)
{
continue;
}
foreach(XmlNode toImport in value2.ChildNodes)
{
if (toImport.Name == "id")
continue;
value1.AppendChild(xmlDocument1.ImportNode(toImport, true));
}
}
}
Console.WriteLine(xmlDocument1.InnerXml);
希望对您有所帮助。
string xml1 = @"
<OutLooksync>
<value>
<id>1</id>
<name>AAA</name>
</value>
<value>
<id>2</id>
<name>bbb</name>
</value>
<value>
<id>3</id>
<name>ccc</name>
</value>
</OutLooksync>";
string xml2 = @"
<OutLooksync>
<value>
<id>4</id>
<Company>abc</Company>
<address>ASD</address>
</value>
</OutLooksync>";
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(xml1);
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(xml2);
XmlNode newvalue = doc1.ImportNode(doc2.DocumentElement.FirstChild, true);
doc1.DocumentElement.AppendChild(newvalue);
xml1:
<value>
<Id>1</id>
<name>AAA</name>
</value>
<value>
<Id>2</id>
<name>bbb</name>
</value>
<value>
<Id>3</id>
<name>ccc</name>
</value>
xml2:
<value>
<Id>1</id>
<Company>abc</Company>
<address>ASD</address>
</value>
result :
<value>
<Id>1</id>
<name>AAA</name>
<Company>abc</Company>
<address>ASD</address>
</value>
<value>
<Id>2</id>
<name>bbb</name>
</value>
<value>
<Id>3</id>
<name>ccc</name>
</value>
if id of 1st xml = 2nd xml then merge the 2nd xml record with 1st xml value record. 在我的代码中,我没有检查 id 值,但我只是试图将第二条 xml 记录与 root 和 1xml 记录合并。如果你能帮助我,那就太好了。我得到错误有要插入的节点来自不同的文档上下文。
XmlNode x1 = doc1.SelectSingleNode("/OutLooksync/value");
foreach (XmlNode node in x1.SelectNodes("/OutLooksync/value"))
{
x1.AppendChild(doc2.ImportNode(node, true));
}
XmlNodeList x1 = doc1.SelectSingleNodes("/OutLooksync/value");
foreach (XmlNode node in x1)
{
doc2.appendChild(node);
}
通过使用 XmlDocument
,
- 您可以在
xml1
中的 values 和xml2
[=33 中的 values 中循环=] - 检查
id
是否等于,并从xml2
导入
childNodes
1 - Xml 用于测试:
string xml1 = @"
<OutLooksync>
<value>
<id>1</id>
<name>AAA</name>
</value>
<value>
<id>2</id>
<name>bbb</name>
</value>
<value>
<id>3</id>
<name>ccc</name>
</value>
</OutLooksync>";
string xml2 = @"
<OutLooksync>
<value>
<id>1</id>
<Company>abc</Company>
<address>ASD</address>
</value>
</OutLooksync>";
2 - 导入节点的代码
XmlDocument xmlDocument1 = new XmlDocument();
xmlDocument1.LoadXml(xml1);
XmlDocument xmlDocument2 = new XmlDocument();
xmlDocument2.LoadXml(xml2);
XmlNodeList values1 = xmlDocument1.SelectNodes("/OutLooksync/value");
XmlNodeList values2 = xmlDocument2.SelectNodes("/OutLooksync/value");
foreach(XmlNode value1 in values1)
{
foreach(XmlNode value2 in values2)
{
if(value1.SelectSingleNode("./id").InnerText != value2.SelectSingleNode("./id")?.InnerText)
{
continue;
}
foreach(XmlNode toImport in value2.ChildNodes)
{
if (toImport.Name == "id")
continue;
value1.AppendChild(xmlDocument1.ImportNode(toImport, true));
}
}
}
Console.WriteLine(xmlDocument1.InnerXml);
希望对您有所帮助。
string xml1 = @"
<OutLooksync>
<value>
<id>1</id>
<name>AAA</name>
</value>
<value>
<id>2</id>
<name>bbb</name>
</value>
<value>
<id>3</id>
<name>ccc</name>
</value>
</OutLooksync>";
string xml2 = @"
<OutLooksync>
<value>
<id>4</id>
<Company>abc</Company>
<address>ASD</address>
</value>
</OutLooksync>";
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(xml1);
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(xml2);
XmlNode newvalue = doc1.ImportNode(doc2.DocumentElement.FirstChild, true);
doc1.DocumentElement.AppendChild(newvalue);