Reputation: 35
I would like to read all the attributes value specified in the xml.Please find below the sample xml as follows:
<DrWatson>
<Bugs Name="STATE" TestCondition="STATE">
<Bug>
<family>ESG</family>
<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>
<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>
<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 would like to read all the attributes mention under Test Condition
in one go.
Currently i am trying to perform this operation using the following code:
XmlDocument XDoc = new DrWatsonCore().LoadXMLFromFile(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");
string attrVal_New = "";
int m = 0;
attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;
After using this code i am only able to read only one attribute as "STATE" whereas there is another attribute as "STATUS" is not getting read. Please suggest.
Upvotes: 2
Views: 197
Reputation: 236218
With LINQ to XML you can select all distinct test condition values:
var xdoc = XDocument.Load(path_to_xml);
var testConditions = xdoc.Root.Elements("Bugs")
.Select(b => (string)b.Attribute("TestCondition"))
.Distinct();
Output:
STATE
STATUS
Upvotes: 1
Reputation: 6442
Output from code below:
Name:STATE
TestCondition:STATE
Name:STATUS
TestCondition:STATUS
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(@"
<DrWatson>
<Bugs Name=""STATE"" TestCondition=""STATE"">
<Bug>
<family>ESG</family>
<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>
<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>
<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>");
foreach( XmlNode a in xdoc.SelectNodes( "//*/@*")){
Console.Out.WriteLine( a.Name + ":" + a.Value );
}
Upvotes: 0