2

我想读取xml中指定的所有属性值。请在示例xml下面找到如下:

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

我想一口气阅读下面提到的所有属性Test Condition。目前我正在尝试使用以下代码执行此操作:

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;

使用此代码后,我只能将一个属性读取为“STATE”,而另一个属性为“STATUS”没有被读取。请建议。

4

2 回答 2

1

使用 LINQ to XML,您可以选择所有不同的测试条件值:

var xdoc = XDocument.Load(path_to_xml);
var testConditions = xdoc.Root.Elements("Bugs")
                         .Select(b => (string)b.Attribute("TestCondition"))
                         .Distinct();

输出:

STATE
STATUS
于 2013-09-13T13:09:57.700 回答
0

以下代码的输出:

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 );
}
于 2013-09-13T13:08:57.013 回答