简体   繁体   中英

How can i pick the comma separated attribute value in xml one by one as loop in c#

I would like to use comma separated attribute values defined under TestCondition in xml one by one in loop for further execution of my code.Please find the sample xml as follows:

    <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>

I am calling the individual attribute value in my code as follows:

attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;
string attributelowercase = attrVal_New.ToLower();

m++;

what i want is to use 'STATE' and 'STATUS' available under TestCondition one by one.Please suggest.

Assuming your real xml is valid

string[] vals = XDocument.Load(fName)
                    .Root
                    .Element("Bugs")
                    .Attribute("TestCondition")
                    .Value.Split(',');

You can use a foreach loop and the string.Split method like so:

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(','));
}

The string.Split()-method splits your string into an array at the char passed as argument, so

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

returns

new string[] {"STATE","STATUS","HELP","WHATEVER"};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM