How to read a particular node from xml in C#?

.net, asp.net, c#

Solution

I found a simple solution for my problem

XmlNodeList xnList = doc.SelectNodes("/Loop/Loop/Segment[@Name='AAA']");
          foreach (XmlNode xn in xnList)
          {
              if (xn.HasChildNodes)
              {
                  foreach (XmlNode item in xn.ChildNodes)
                  {
                      Console.WriteLine(item.InnerText);
                  }
              }  
          }

Problem

I have following XML: ``` <Loop Name="MasterData"> <Loop Name="SlaveData"> <Segment Name="AAA"> <Node1>hello</Node1> <Node2>john</Node2> <Node3>hi</Node3> <Node4>marry</Node4> </Segment> <Segment Name="BBB"> <Node1>00</Node1> <Node2> </Node2> <Node3>00</Node3> <Node4> </Node4> </Segment> </Loop> </Loop> ``` I have to read value of each Node i.e, Node1, Node2, Node3, Node4 which are under Segment node whose attribute ie `Name = "AAA"`. How can I do this. I am referring following link from stackoverflow but thats not working for me. How to read attribute value from XmlNode in C#? I need output like this Lets I have four sting variables `strNode1, strNode2, strNode3, strNode4`. I want to store values in above four variables like following ``` strNode1 = "hello" strNode2 = "john" strNode3 = "hi" strNode4 = "marry" ```

Original source

Related problems