xml属性名不变但不同节点的值发生变化时,如何替换xml中的属性值
How to replace the attribute value in xml when the attribute name remains same but value of it changes in different nodes
我有一个 xml 并且想在我找到它的任何地方替换属性值,元素名称和属性名称保持不变,但要替换的属性值取决于其当前值。
原xml:
<Loan>
<status="First">
<report active = "True" raw_xml = "My name is abc and I am a doctor"/>
</status>
<status="Second">
<report active = "True" raw_xml = "My name is def and I am an actor"/>
</status>
<status="Third">
<report active = "True" raw_xml = "My name is xyz and I am a coder"/>
</status>
</Loan>
希望输出为:
<Loan>
<status="First">
<report active = "True" raw_xml = "My doctor"/>
</status>
<status="Second">
<report active = "True" raw_xml = "My actor"/>
</status>
<status="Third">
<report active = "True" raw_xml = "My coder"/>
</status>
</Loan>
我正在做一些操作来提取 raw_xml 的部分并替换它的当前值。
我有提取和替换值的代码。
但是当我使用方法 Single/Default 时它失败并出现错误
Sequence contains more than one matching element
如何通过它并循环遍历它并使用 Xdocument.descendants 替换值..我不想使用 Xpath 因为我正在处理的真实 xml 有这么多其中的内部节点并为每个属性获取 xpath 真的很难。
我用来替换的当前代码。
foreach (var report in doc.Descendants("report"))
{
var xms = "";
xms = report.Attribute("raw_xml").Value;
//My code to change to extract the required attribute value goes here..creating an xmsdoc variable and storing the output value for attribute in it
var element = doc.Descendants("report").Single(x => x.Attribute("active").Value == "True");
element.SetAttributeValue("raw_xml", xmsdoc.ToString());
}
好的,我创建了一个可编译的示例:
test.xml
(属性修复命名为 id
)
<Loan>
<status id="First">
<report active = "True" raw_xml = "My name is abc and I am a doctor"/>
</status>
<status id="Second">
<report active = "True" raw_xml = "My name is def and I am an actor"/>
</status>
<status id="Third">
<report active = "True" raw_xml = "My name is xyz and I am a coder"/>
</status>
</Loan>
Program.cs
using System.Xml.Linq;
namespace ConsoleApplication99
{
class Program
{
static string FixXmlString(string value)
{
int namePos = value.IndexOf(" name ");
if (namePos < 0) return value; // name not found
int lastSpace = value.LastIndexOf(' ');
int cutLength = lastSpace - namePos;
return value.Remove(namePos, cutLength);
}
static void Main()
{
XDocument doc = XDocument.Load("test.xml");
foreach (XElement x in doc.Descendants("report").Where(x => x.Attribute("active").Value == "True"))
{
var attribute = x.Attribute("raw_xml");
if (attribute != null)
{
attribute.Value = FixXmlString(attribute.Value);
}
}
doc.Save("new.xml");
}
}
}
结果 new.xml
:
<Loan>
<status id="First">
<report active="True" raw_xml="My doctor" />
</status>
<status id="Second">
<report active="True" raw_xml="My actor" />
</status>
<status id="Third">
<report active="True" raw_xml="My coder" />
</status>
</Loan>
编辑:添加缺失的"active"检查
我有一个 xml 并且想在我找到它的任何地方替换属性值,元素名称和属性名称保持不变,但要替换的属性值取决于其当前值。
原xml:
<Loan>
<status="First">
<report active = "True" raw_xml = "My name is abc and I am a doctor"/>
</status>
<status="Second">
<report active = "True" raw_xml = "My name is def and I am an actor"/>
</status>
<status="Third">
<report active = "True" raw_xml = "My name is xyz and I am a coder"/>
</status>
</Loan>
希望输出为:
<Loan>
<status="First">
<report active = "True" raw_xml = "My doctor"/>
</status>
<status="Second">
<report active = "True" raw_xml = "My actor"/>
</status>
<status="Third">
<report active = "True" raw_xml = "My coder"/>
</status>
</Loan>
我正在做一些操作来提取 raw_xml 的部分并替换它的当前值。
我有提取和替换值的代码。
但是当我使用方法 Single/Default 时它失败并出现错误
Sequence contains more than one matching element
如何通过它并循环遍历它并使用 Xdocument.descendants 替换值..我不想使用 Xpath 因为我正在处理的真实 xml 有这么多其中的内部节点并为每个属性获取 xpath 真的很难。
我用来替换的当前代码。
foreach (var report in doc.Descendants("report"))
{
var xms = "";
xms = report.Attribute("raw_xml").Value;
//My code to change to extract the required attribute value goes here..creating an xmsdoc variable and storing the output value for attribute in it
var element = doc.Descendants("report").Single(x => x.Attribute("active").Value == "True");
element.SetAttributeValue("raw_xml", xmsdoc.ToString());
}
好的,我创建了一个可编译的示例:
test.xml
(属性修复命名为 id
)
<Loan>
<status id="First">
<report active = "True" raw_xml = "My name is abc and I am a doctor"/>
</status>
<status id="Second">
<report active = "True" raw_xml = "My name is def and I am an actor"/>
</status>
<status id="Third">
<report active = "True" raw_xml = "My name is xyz and I am a coder"/>
</status>
</Loan>
Program.cs
using System.Xml.Linq;
namespace ConsoleApplication99
{
class Program
{
static string FixXmlString(string value)
{
int namePos = value.IndexOf(" name ");
if (namePos < 0) return value; // name not found
int lastSpace = value.LastIndexOf(' ');
int cutLength = lastSpace - namePos;
return value.Remove(namePos, cutLength);
}
static void Main()
{
XDocument doc = XDocument.Load("test.xml");
foreach (XElement x in doc.Descendants("report").Where(x => x.Attribute("active").Value == "True"))
{
var attribute = x.Attribute("raw_xml");
if (attribute != null)
{
attribute.Value = FixXmlString(attribute.Value);
}
}
doc.Save("new.xml");
}
}
}
结果 new.xml
:
<Loan>
<status id="First">
<report active="True" raw_xml="My doctor" />
</status>
<status id="Second">
<report active="True" raw_xml="My actor" />
</status>
<status id="Third">
<report active="True" raw_xml="My coder" />
</status>
</Loan>
编辑:添加缺失的"active"检查