c# - 为什么 XmlDocument 声明变量 A 在 B XmlDocument 声明变量被修改时发生变化?
问题描述
我收到一个基本 XML 文件,我需要创建 N 个具有不同内容值的 XmlFiles。基本上我做了一个副本,更改一些节点值并创建新文件而不修改基本文件。
我将每个 XML 文档添加到文档列表中以执行其他过程,然后进行交互并创建 N 个文件。在我的代码执行后,我最终得到了所有具有相同信息的文件,即使是基础文件也被修改了。我创建了一个基本代码来演示它。感谢您解释为什么会发生这种情况。
// file1.xml
<?xml version="1.0" encoding="UTF-16"?>
<BOM>
<BO>
<AdmInfo>
<Object>2</Object>
<Version>2</Version>
</AdmInfo>
<BusinessPartners>
<row>
<CardCode>111111</CardCode>
<CardName>MADERAS DE AGUADULCE, S.A</CardName>
<GroupCode>P-Locales</GroupCode>
</row>
</BusinessPartners>
</BO>
</BOM>
// C# code - method that change the value into the xmlFile.
public XmlDocument ChangeValues(XmlDocument document, List<Tuple<string, string>> AtriValues )
{
XmlDocument NewXMLDocument = new XmlDocument();
// pass the content to another XmlDocument
NewXMLDocument = document;
foreach (var Atribute in AtriValues)
{
XmlElement root = NewXMLDocument.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName(Atribute.Item1.ToString());
IEnumerator ienum = elemList.GetEnumerator();
while (ienum.MoveNext())
{
XmlNode title = (XmlNode)ienum.Current;
// Console.WriteLine(title.InnerText);
title.InnerText = Atribute.Item2.ToString();
//xn[Atribute.Item1.ToString()].InnerText = Atribute.Item2.ToString();
}
}
return NewXMLDocument;
}
// C# code- the main prg
static void Main(string[] args)
{
Util2 Util = new Util2();
List<XmlDocument> Documents = new List<XmlDocument>();
XmlDocument xmlDocument = new XmlDocument();
// load the XML file
xmlDocument.Load(@"C:\WIP\BaSe\TEST\file1.xml");
// Save the base file
Documents.Add(xmlDocument);
// Change the content of the document to create document A
List<Tuple<string, string>> AtriValuesA = new List<Tuple<string, string>>();
AtriValuesA.Add(new Tuple<string, string>("CardCode", "9999"));
AtriValuesA.Add(new Tuple<string, string>("GroupCode", "AA"));
Documents.Add(Util.ChangeValues(xmlDocument, AtriValuesA));
// Change the content of the document to create document B
List<Tuple<string, string>> AtriValuesB = new List<Tuple<string, string>>();
AtriValuesB.Add(new Tuple<string, string>("CardCode", "2222"));
AtriValuesB.Add(new Tuple<string, string>("GroupCode", "BB"));
Documents.Add(Util.ChangeValues(xmlDocument, AtriValuesB));
// get the document and then save then
Documents[0].Save(@"C:\WIP\BaSe\TEST\base.xml");
Documents[1].Save(@"C:\WIP\BaSe\TEST\DOCA.xml");
Documents[2].Save(@"C:\WIP\BaSe\TEST\DOCB.xml");
}
解决方案
所有文件都在更改,因为您正在更改 ChangeValues 函数中的原始 xml 文档。下面的分配(新文档)无效,因为您正在将原始文档的引用分配给它。
XmlDocument NewXMLDocument = new XmlDocument();
// pass the content to another XmlDocument
NewXMLDocument = document;
我想到的最快的解决方案是首先使用File.Copy
函数将原始文件复制到新文件名。之后,只需将新文件加载到文档中并进行更改。
按照您的示例,代码将类似于:
var baseFile = @"C:\WIP\BaSe\TEST\base.xml";
var doc1 = @"C:\WIP\BaSe\TEST\DOCA.xml";
var doc2 = @"C:\WIP\BaSe\TEST\DOCB.xml";
File.Copy(baseFile, doc1);
File.Copy(baseFile, doc2);
// you might copy this to other function.
XmlDocument xmlDocument1 = new XmlDocument();
xmlDocument1.Load(doc1);
Util.ChangeValues(xmlDocument1, AtriValuesA);
xmlDocument1.Save(doc1);
推荐阅读
- gridview - 不同布局下flutter gridview子高度如何实现?
- javascript - React JS setState 行为
- java - 从另一个片段调用方法
- java - 在 Java 中向 Rest Web 服务发送 JSON Post 请求
- angular - 如何从 html 文件中调用服务方法。?
- python - 线性回归怀疑。手动验证值
- javascript - 使用 jquery 捕获所有单击事件,无论元素单击事件是否具有 stopPropagation
- animation - ThreeJS GLB IK 动画导出
- c# - 如何在 EF / EF Core 的第二个表上实现具有某些条件的左连接?
- javascript - 如何使用 Reactjs 代码开发 windows 桌面应用程序