如何使用c#一次性读取xml中提到的所有属性

时间:2013-09-13 12:54:35

标签: c# xml

我想读取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;

使用此代码后,我只能读取一个属性为&#34; STATE&#34;而另一个属性是&#34;状态&#34;没有得到阅读。 请建议。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 );
}