C# 如何在xml中逐个选取逗号分隔的属性值作为c中的循环#
我想在循环中使用xml中的C# 如何在xml中逐个选取逗号分隔的属性值作为c中的循环#,c#,xml,entity-attribute-value,C#,Xml,Entity Attribute Value,我想在循环中使用xml中的TestCondition下定义的逗号分隔属性值,以便进一步执行我的代码。请查找示例xml,如下所示: <DrWatson> <Bugs Name="Testing New things" TestCondition="STATE,STATUS"> <Bug> <family>ESG</family> <product>Dr.Watson</prod
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
下逐个使用可用的“STATE”和“STATUS”。请建议。假设您的真实xml是有效的
string[] vals = XDocument.Load(fName)
.Root
.Element("Bugs")
.Attribute("TestCondition")
.Value.Split(',');
您可以使用foreach循环和string.Split方法,如下所示:
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(','));
}
返回
new string[] {"STATE","STATUS","HELP","WHATEVER"};
你试过谷歌搜索吗?“XML属性”部分与您的问题完全无关,特别是因为您已经解决了它。实际上,在TestCondition下,XML中还有其他属性可用,我希望一次读取所有属性,以便在代码中使用for循环进一步迭代它们。
new string[] {"STATE","STATUS","HELP","WHATEVER"};