用 c sharp 查找和替换 XML 中的标签值
Finding and replacing values of a tag in XML with c sharp
<TestCase Name="DEBUG">
<ActionEnvironment Name="Carved records indication">
<Define Name="_TestedVersionPath" Value="{CustomParam {paramName=PA tested version installer folder path}, {appName=PA installer}, {hint=\ptnas1\builds\Temp Builds\Forensic\Physical Analyzer\PA.Test\UFED_Analyzer_17.02.05_03-00_6.0.0.128\EncryptedSetup}}"/>
<Define Name="_PathOfdata" Value="SharedData\myfolder\mydata.xml"/>
<ActionSet Name="DEBUG">
<Actions>
<SpecialAction ActionName="myactionname">
<CaseName>123</CaseName>
<UaeSendQueryValues>
<URL>192.168.75.133</URL>
<RestURL></RestURL>
<UserName>user1</UserName>
<Password>aaa</Password>
<PathOfQuery>_PathOfdata</PathOfQuery>
<Method>GET</Method>
<ParamsFromFile></ParamsFromFile>
</UaeSendQueryValues>
</SpecialAction>
</Actions>
</ActionSet>
</ActionEnvironment>
我有以上xml。我需要找到每个 PathOfQuery 标签,获取它的文本(在示例 _PathOfdata 中)然后在 xml 树中找到第一个 Define 标签 who's name = to text of PathofQuery 标签并获取其值(在示例中 "SharedData\myfolder\mydata.xml")
然后我想用另一个字符串替换那个值。
我需要为 xml 中出现的每个 PathofQuery 标签(可能不止一个)执行此操作,并且我想始终找到 Define 标签的第一个出现(可能不止一个) ) 当我从发现 PathofQuery 标记的点向上移动树时。
我想在 C Sharp 上执行此操作
任何帮助将不胜感激。
我们假设 string s
满足上述 Xml。那么以下代码将为您工作:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(s);
XmlNode pathOfQuery = xDoc.SelectSingleNode("//PathOfQuery");
string pathOfQueryValue = pathOfQuery.InnerText;
Console.WriteLine(pathOfQueryValue);
XmlNode define = xDoc.SelectSingleNode("//Define[@Name='" + pathOfQueryValue + "']");
if(define!=null)
{
string defineTagValue = define.Attributes["Value"].Value;
Console.WriteLine(defineTagValue);
pathOfQuery.InnerText = defineTagValue;
Console.WriteLine(pathOfQuery.InnerText);
}
<TestCase Name="DEBUG">
<ActionEnvironment Name="Carved records indication">
<Define Name="_TestedVersionPath" Value="{CustomParam {paramName=PA tested version installer folder path}, {appName=PA installer}, {hint=\ptnas1\builds\Temp Builds\Forensic\Physical Analyzer\PA.Test\UFED_Analyzer_17.02.05_03-00_6.0.0.128\EncryptedSetup}}"/>
<Define Name="_PathOfdata" Value="SharedData\myfolder\mydata.xml"/>
<ActionSet Name="DEBUG">
<Actions>
<SpecialAction ActionName="myactionname">
<CaseName>123</CaseName>
<UaeSendQueryValues>
<URL>192.168.75.133</URL>
<RestURL></RestURL>
<UserName>user1</UserName>
<Password>aaa</Password>
<PathOfQuery>_PathOfdata</PathOfQuery>
<Method>GET</Method>
<ParamsFromFile></ParamsFromFile>
</UaeSendQueryValues>
</SpecialAction>
</Actions>
</ActionSet>
</ActionEnvironment>
我有以上xml。我需要找到每个 PathOfQuery 标签,获取它的文本(在示例 _PathOfdata 中)然后在 xml 树中找到第一个 Define 标签 who's name = to text of PathofQuery 标签并获取其值(在示例中 "SharedData\myfolder\mydata.xml")
然后我想用另一个字符串替换那个值。
我需要为 xml 中出现的每个 PathofQuery 标签(可能不止一个)执行此操作,并且我想始终找到 Define 标签的第一个出现(可能不止一个) ) 当我从发现 PathofQuery 标记的点向上移动树时。
我想在 C Sharp 上执行此操作
任何帮助将不胜感激。
我们假设 string s
满足上述 Xml。那么以下代码将为您工作:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(s);
XmlNode pathOfQuery = xDoc.SelectSingleNode("//PathOfQuery");
string pathOfQueryValue = pathOfQuery.InnerText;
Console.WriteLine(pathOfQueryValue);
XmlNode define = xDoc.SelectSingleNode("//Define[@Name='" + pathOfQueryValue + "']");
if(define!=null)
{
string defineTagValue = define.Attributes["Value"].Value;
Console.WriteLine(defineTagValue);
pathOfQuery.InnerText = defineTagValue;
Console.WriteLine(pathOfQuery.InnerText);
}