如何使用c#读取各个属性值的所有xml节点

时间:2013-09-14 07:06:51

标签: c# xml

我想执行不同的xml节点集,这些节点由xml中各自的属性值标识。 但我面临的是,即使正在识别第二个属性值,也只会执行设置的1 xml节点。这是我当前的代码:

for (int m = 0; m < 10; m++)
{
    attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;
    foreach (string attr in attrVal_New.Split(','))
    {
        Console.WriteLine(attr);
        ....

请按以下方式找到示例xml:

<DrWatson>
  <Bugs Name="Testing 11" TestCondition="STATE">
    <Bug>
      <family>ESG</family>
      <product>Dr.Watson</product>
      <duplicateId>Blank</duplicateId>
      <note></note>
    </Bug>
    <Bug>
          <family>ESG</family>
          <product>Dr.Watson</product>
          <duplicateId>Blank</duplicateId>
          <note></note>
    </Bug>
  </Bugs>
  <Bugs Name="Testing 22" TestCondition="STATUS">
    <Bug>
          <family>ESG</family>
          <product>Dr.Watson</product>
          <duplicateId>Blank</duplicateId>
          <note></note>
    </Bug>
    <Bug>
    <family>ESG</family>
          <product>Dr.Watson</product>
          <duplicateId>Blank</duplicateId>
          <note></note>
    </Bug>
  </Bugs>
</DrWatson>

请注意,TestCondition下定义的属性值为'STATE'和STATUS。当第二次运行此循环时,属性值被检测为'STATUS',但它执行'STATE'属性值下的xml节点。请建议。

以下是“更新错误”的代码段:

 XmlDocument XDoc = new DrWatsonCore().LoadXMLFromFile(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile);
                XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");

我正在使用此部分来识别xml中Attribute Tag Names下的xml中的'TestCondition'

这是我在你的建议后我正在做的事情,我再次遇到同样的问题,因为我正在获取第二个属性值,但STATE属性值下可用的xml节点集正在执行。

for (int m = 0; m < 10; m++)
                {
                    XmlAttributeCollection coll = Update_Bugs.Item(m).Attributes;
                    string value = coll.Item(m).Value;
                attrVal_New = Update_Bugs[m].Attributes["TestCondition"].Value;

                //m++;
                foreach (string attr in attrVal_New.Split(','))
                {                        

                        string attributelowercase = attr.ToLower();                        
                        //Step1: Create Bugs
                        List<string> BugWSResponseList1 = new List<string>();                        
                        BugWSResponseList1 = CreateBugs(FilePath_EXPRESS_API_BugAdd_CreateBugs_DataFile, newValue);

2 个答案:

答案 0 :(得分:1)

    foreach (XmlElement xmlElement in nodeList)
            {
                foreach (XmlElement xmlElement1 in xmlElement.ChildNodes)
                {
                    foreach (XmlElement xmlElement2 in xmlElement1.ChildNodes)
                    {
                        string value = xmlElement2.InnerText;
                        Debug.WriteLine(value);
                    }
                }
            }

输出:

ESG
Dr.Vatson
Blank

ESG
Dr.Hello
Blank

ESG
Dr.Vikram
Blank

ESG
Dr.Watson
Blank

答案 1 :(得分:0)

我不知道为什么tou有一个for循环或为什么你有属性值的分裂,但我会写这样的代码

//Get all the bugs.
XmlNodeList Update_Bugs = XDoc.GetElementsByTagName("Bugs");

//Loop through the bugs
foreach(XmlNode updateBug in Update_Bugs)
{
    //Check for the test condition
    if (updateBug.Attributes["TestCondition"] != null)
    {
        //Get the value of TestCondition.
        string value = updateBug.Attributes["TestCondition"].Value;
        string attributelowercase = value.ToLower();
        Console.WriteLine(attributelowercase);
        //Get the children of the node. This will be the <Bug> nodes.
        XmlNodeList newValue = updateBug.ChildNodes;
        //Get the xml string of the bugs
        string xmlString = updateBug.InnerXml;
        Console.WriteLine(xmlString);
    }
}

这将为您提供TestCondotion和标签之间的XML作为输出,例如:

<Bug>
  <family>ESG</family>
  <product>Dr.Watson</product>
  <duplicateId>Blank</duplicateId>
  <note></note>
</Bug>
<Bug>
  <family>ESG</family>
  <product>Dr.Watson</product>
  <duplicateId>Blank</duplicateId>
  <note></note>
</Bug>

如何处理这取决于你想要用它做什么。