0

我想TestCondition在循环中使用在xml中定义的逗号分隔的属性值来进一步执行我的代码。请找到示例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一一使用“状态”和“状态”。请建议。

4

2 回答 2

1

假设您的真实 xml 是有效的

string[] vals = XDocument.Load(fName)
                    .Root
                    .Element("Bugs")
                    .Attribute("TestCondition")
                    .Value.Split(',');
于 2013-09-13T08:28:43.797 回答
1

您可以像这样使用 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(','));
}

string.Split() 方法在作为参数传递的 char 处将您的字符串拆分为一个数组,因此

"STATE,STATUS,HELP,WHATEVER".Split(',') 

返回

new string[] {"STATE","STATUS","HELP","WHATEVER"};
于 2013-09-13T08:29:13.167 回答