简体   繁体   中英

How to read all xml nodes for respective Attribute values using c#

I would like to execute different set of xml nodes which are being identified by their respective attributes values in xml. But what i am facing is that only the set 1 xml nodes are getting executed even the second attribute value is being identified.Here is my current code:

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);
        ....

Please find the sample xml as follows:

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

Please note that there are different attribute value defined under TestCondition as 'STATE' and STATUS. when running this loop second time the attribute value is being detected as 'STATUS' but it executes the xml nodes which are present under 'STATE' attribute value.Please suggest.

Here is the code snippet for 'Update Bugs' as follows:

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

i am using this part to identify the Attribute Tag Names in the xml available under 'TestCondition' in my xml.

This is what i am doing after your suggestion and i am facing the same issue again as i am picking up the second attribute value but the set of xml nodes available under STATE attribute value is getting executed.

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);
    foreach (XmlElement xmlElement in nodeList)
            {
                foreach (XmlElement xmlElement1 in xmlElement.ChildNodes)
                {
                    foreach (XmlElement xmlElement2 in xmlElement1.ChildNodes)
                    {
                        string value = xmlElement2.InnerText;
                        Debug.WriteLine(value);
                    }
                }
            }

Output :

ESG
Dr.Vatson
Blank

ESG
Dr.Hello
Blank

ESG
Dr.Vikram
Blank

ESG
Dr.Watson
Blank

I do not knwo why tou have a for loop or why you have the split on the attribute value, but I would write the code like this

//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);
    }
}

This will give you as output the TestCondotion and the XML between the tags eg:

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

How to process this depends on wht you want do do with it.

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