3

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);
17
  • It would be useful to show type and code used to obtain Update_Bugs object. Commented Sep 14, 2013 at 7:17
  • I've edited your sample and removed everything that either commented out or not using Update_Bugs - it looks like you did not post portion of the code that should be using Update_Bugs[m] node... Commented Sep 14, 2013 at 7:22
  • Update_Bugs is XMLNode? Commented Sep 14, 2013 at 7:22
  • @ Alexei Levenkov Please find my updated code with the code snippet for 'Update_Bugs' Commented Sep 14, 2013 at 7:26
  • @Vikram Bose yes thats right.. Update_Bugs is the xml node identified on the basis of Attribute Value Tag Name under 'TestCondition' within xml. Commented Sep 14, 2013 at 7:33

2 Answers 2

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

Output :

ESG
Dr.Vatson
Blank

ESG
Dr.Hello
Blank

ESG
Dr.Vikram
Blank

ESG
Dr.Watson
Blank
Sign up to request clarification or add additional context in comments.

2 Comments

Please find my updated code snippet after using your workaround.
My Mis.. Please find the updated code i am using for trying your workaround
0

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 e.g.:

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

1 Comment

How can i get the newvalue response as xml document not as string.Please suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.