Solved

Issue in parsing xml using C#

Posted on 2009-05-05
4
330 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:yamurthy
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:abel
abel earned 100 total points
ID: 24303370
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
 
LVL 6

Accepted Solution

by:
openshac earned 400 total points
ID: 24303401
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
 
LVL 1

Author Closing Comment

by:yamurthy
ID: 31577950
I finally used openshac's solution. this worked fine and reduced my code size and simplified code reading!!! :) Thanks for your replies
0
 
LVL 39

Expert Comment

by:abel
ID: 24327831
You're welcome :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now