Issue in parsing xml using C#

Hi all,
I am facing some problem in getting the attribute values from a xml file using C#. I have attached the code snippet.

My program accepts an input structure from an xml file (see attachment). It parses the file and then gets all the column names that is in turn used to create columns in Grid view control. The issue is... I am not able to get the column names if the node has some sub nodes.

In the attached xml file, you can see the <member Name"elemDesc" type="unsigned int"> has one or more sub nodes <Value></Value>. In this case the column names are not getting displayed.
But works fine if the node does not have any sub nodes as in <member Name = "Type" type = "unsigned int" />

I am sure the problem lies in the expression passed. Please help me resolve this issue. without this I will not be able to proceed with my work comfortable :(

private void ReadInputStructure(ref List<string> Items, string deviceName)
        {            
            try
            {   
                string fileName = "D:\\Ultrasound\\Initiatives\\StructureInput.xml";
                XPathDocument doc = new XPathDocument(fileName);
                XPathNavigator nav = doc.CreateNavigator();
 
                // Compile a standard XPath expression
                XPathExpression expr;
                expr = nav.Compile("/devices/device[@Name=\"" + deviceName + "\"]/member");
                XPathNodeIterator iterator = nav.Select(expr);                
                while (iterator.MoveNext())
				{
					XPathNavigator nav2 = iterator.Current.Clone();                    
					nav2.MoveToFirstChild();
                    string ColumnName = nav2.GetAttribute("Name", nav2.NamespaceURI);                    
                    Items.Add(ColumnName);  
				}                
			}
			catch(Exception ex) 
			{
				Console.WriteLine(ex.Message);
			}            
        }

Open in new window

StructureInput.txt
LVL 1
Anjeneya MurthySr. ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abelCommented:
It seems that all you are interested in is the Name attribute, correct? Why not change your XPath to only select the name attributes, like this?:

expr = nav.Compile("/devices/device[@Name=\"" + deviceName + "\"]/member/@Name");
0
openshacCommented:
Can you give the output you are after, I'm a little unsure of what you want.

I don't know if the following will work:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
 
foreach(XmlNode node in xmlDoc.SelectNodes("/devices/device[@Name=\"" + deviceName + "\"]/member"))
{
    Items.Add(node.Attributes["Name"].Value);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anjeneya MurthySr. ConsultantAuthor Commented:
I finally used openshac's solution. this worked fine and reduced my code size and simplified code reading!!! :) Thanks for your replies
0
abelCommented:
You're welcome :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.