如何使用XDocument更新DataSet.WriteXml()生成的XML文件
基于XDocument操作DataSet生成的XML文件方案
背景
你所操作的XML由DataSet的WriteXml()方法生成,生成代码为:QCSavedData.WriteXml(@strQCViewAllPath),其中QCSavedData为DataSet对象,XML结构示例如下:
<?xml version="1.0" standalone="yes"?> <HNNMY_ViewAll> <dgvViewAll_Vertical> <Section_x0020_>MS</Section_x0020_> <LineItem>Morgan Stanley</LineItem> <Revise_x0020_Date>10-09-2020</Revise_x0020_Date> <_x0032_010_x0020_FYA>126,966.0000</_x0032_010_x0020_FYA> <_x0032_011_x0020_FYA>128,810.0000</_x0032_011_x0020_FYA> <_x0032_012_x0020_FYA>140,948.0000</_x0032_012_x0020_FYA> <_x0032_013_x0020_FYA>150,090.0000</_x0032_013_x0020_FYA> <_x0031_Q_x0020_2014A>37,524.0000</_x0031_Q_x0020_2014A> <_x0032_Q_x0020_2014A>44,181.0000</_x0032_Q_x0020_2014A> <_x0033_Q_x0020_2014A>45,259.0000</_x0033_Q_x0020_2014A> <_x0034_Q_x0020_2014A>49,656.0000</_x0034_Q_x0020_2014A> <_x0032_014_x0020_FYA>176,620.0000</_x0032_014_x0020_FYA> <_x0031_Q_x0020_2015A>46,791.0000</_x0031_Q_x0020_2015A> <_x0032_Q_x0020_2015A>53,233.0000</_x0032_Q_x0020_2015A> <_x0033_Q_x0020_2015A>53,420.0000</_x0033_Q_x0020_2015A> <_x0034_Q_x0020_2015A>56,477.0000</_x0034_Q_x0020_2015A> <_x0032_015_x0020_FYA>209,921.0000</_x0032_015_x0020_FYA> <_x0031_Q_x0020_2016A>50,624.0000</_x0031_Q_x0020_2016A> <_x0032_Q_x0020_2016A>54,341.0000</_x0032_Q_x0020_2016A> <_x0033_Q_x0020_2016A>56,802.0000</_x0033_Q_x0020_2016A> <GroupKey>Consensus Model~Total Revenue Including VAT~RD_001~NBM~~1~MS</GroupKey> </dgvViewAll_Vertical> </HNNMY_ViewAll>
问题解决方案
1. 查询值为MS的Section_x0020_节点
节点名中的x0020是DataSet生成XML时对空格的标准转义,直接使用转义后的完整节点名查询即可,无需额外解码,示例查询逻辑:
// 加载XML文件 XDocument doc = XDocument.Load(@strQCViewAllPath); // 查询Section_x0020_值为MS的所有dgvViewAll_Vertical节点 var targetNodes = doc.Descendants("dgvViewAll_Vertical") .Where(n => n.Element("Section_x0020_")?.Value == "MS");
2. 更新带转义字符的数值节点
.NET的DataSet将列名转为XML节点名时,调用的是XmlConvert.EncodeLocalName()方法做转义,你只需将原始列名传入该方法,就能自动得到转义后的节点名,无需手动硬编码转义字符串:
// 原始列名 string originalColName = "2010 FYA"; // 转义得到XML节点名 string encodedNodeName = XmlConvert.EncodeLocalName(originalColName); // 输出结果为:_x0032_010_x0020_FYA
拿到转义后的节点名后,直接查询对应节点赋值即可完成更新。
3. 完整业务逻辑实现代码
以下是符合要求的完整可运行代码,实现逻辑:匹配Section_x0020_值等于GroupKey按~分割的第6索引位(对应你所说的第5项,按提供的GroupKey示例拆分后第7个元素为MS,和Section值一致),更新2010 FYA对应节点的值:
using System.Xml; using System.Xml.Linq; // 配置参数 string xmlPath = @strQCViewAllPath; string targetColName = "2010 FYA"; string newValue = "999,999.0000"; // 替换为实际需要更新的目标值 // 加载XML XDocument doc = XDocument.Load(xmlPath); // 转义得到目标列对应的XML节点名 string targetNodeName = XmlConvert.EncodeLocalName(targetColName); // 遍历所有行节点 foreach (var rowNode in doc.Descendants("dgvViewAll_Vertical")) { // 获取Section值 string sectionVal = rowNode.Element("Section_x0020_")?.Value; // 获取GroupKey并拆分 string groupKeyVal = rowNode.Element("GroupKey")?.Value; if (string.IsNullOrEmpty(sectionVal) || string.IsNullOrEmpty(groupKeyVal)) continue; string[] groupKeyParts = groupKeyVal.Split('~'); // 匹配条件:Section值等于GroupKey按~拆分的第6索引位 if (groupKeyParts.Length >=7 && sectionVal == groupKeyParts[6]) { // 找到目标列节点 XElement targetColNode = rowNode.Element(targetNodeName); if (targetColNode != null) { // 更新值 targetColNode.Value = newValue; } } } // 保存修改后的XML doc.Save(xmlPath);
注意:如果GroupKey分割项索引和示例有差异,调整
groupKeyParts的索引值即可。
内容的提问来源于stack exchange,提问作者user15940620
相关产品推荐
相关产品推荐

