如何在 c# 中逐个选择xml中的逗号分隔属性值作为循环
本文关键字:属性 分隔 循环 xml 选择 | 更新日期: 2023-09-27 18:31:20
我想在循环中逐个使用在 xml 中TestCondition
下定义的逗号分隔属性值,以进一步执行我的代码。请按如下方式查找示例 xml:
<DrWatson>
<Bugs Name="Testing New things" TestCondition="STATE,STATUS">
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea>Blank</subarea>
<title>Bug.AddNote#1 : Dr.Watson Framework by Aman</title>
<description>test</description>
<appLanguages>English~~Bug</appLanguages>
<platforms>Win XP All~~English~~Bug</platforms>
<state>Open</state>
<status>NeedsReview</status>
<reason>Blank</reason>
<failureType>Unspecified</failureType>
<Frequency>Unknown</Frequency>
<severity>0</severity>
<priority>0</priority>
<methodFound>Blank</methodFound>
<foundInBuild>1</foundInBuild>
<dev>bansal</dev>
<qe>sdawar</qe>
<keyword>Blank</keyword>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea>Blank</subarea>
<title>Bug.AddNote#1 : Dr.Watson Framework by Aman</title>
<description>test</description>
<appLanguages>English~~Bug</appLanguages>
<platforms>Win XP All~~English~~Bug</platforms>
<state>Open</state>
<status>ToFix</status>
<reason>Blank</reason>
<failureType>Unspecified</failureType>
<Frequency>Unknown</Frequency>
<severity>0</severity>
<priority>0</priority>
<methodFound>Blank</methodFound>
<foundInBuild>1</foundInBuild>
<dev>bansal</dev>
<qe>sdawar</qe>
<keyword>Blank</keyword>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
</Bugs>
<Bugs Name="STATUS" TestCondition="STATUS">
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea>Blank</subarea>
<title>Bug.AddNote#1 : Dr.Watson Framework by Aman</title>
<description>test</description>
<appLanguages>English~~Bug</appLanguages>
<platforms>Win XP All~~English~~Bug</platforms>
<state>Open</state>
<status>NeedsReview</status>
<reason>Blank</reason>
<failureType>Unspecified</failureType>
<Frequency>Unknown</Frequency>
<severity>0</severity>
<priority>0</priority>
<methodFound>Blank</methodFound>
<foundInBuild>1</foundInBuild>
<dev>bansal</dev>
<qe>sdawar</qe>
<keyword>Blank</keyword>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
<Bug>
<family>ESG</family>
<product>Dr.Watson</product>
<version>Xpress API</version>
<productarea>1</productarea>
<subarea>Blank</subarea>
<title>Bug.AddNote#1 : Dr.Watson Framework by Aman</title>
<description>test</description>
<appLanguages>English~~Bug</appLanguages>
<platforms>Win XP All~~English~~Bug</platforms>
<state>Open</state>
<status>ToFix</status>
<reason>Blank</reason>
<failureType>Unspecified</failureType>
<Frequency>Unknown</Frequency>
<severity>0</severity>
<priority>0</priority>
<methodFound>Blank</methodFound>
<foundInBuild>1</foundInBuild>
<dev>bansal</dev>
<qe>sdawar</qe>
<keyword>Blank</keyword>
<duplicateId>Blank</duplicateId>
<note></note>
</Bug>
</Bugs>
</DrWatson>
我在代码中调用单个属性值,如下所示:
attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;
string attributelowercase = attrVal_New.ToLower();
m++;
我想要的是一一使用TestCondition
下可用的"状态"和"状态"。请指教。
假设你的真实xml是有效的
string[] vals = XDocument.Load(fName)
.Root
.Element("Bugs")
.Attribute("TestCondition")
.Value.Split(',');
您可以使用 foreach 循环和字符串。拆分方法如下:
XElement bugsElement = XDocument.Load(fName)
.Root
.Element("DrWatson");
List<string> vals = new List<string>();
foreach (XElement el in bugsElement)
{
vals.AddRange(bugsElement.Attribute("TestCondition").Value.Split(','));
}
字符串。Split()-方法将您的字符串拆分为作为参数传递的字符处的数组,因此
"STATE,STATUS,HELP,WHATEVER".Split(',')
返回
new string[] {"STATE","STATUS","HELP","WHATEVER"};