Solved

Looping through an XML document

Posted on 2004-04-04
7
288 Views
Last Modified: 2013-11-19
I have an XML document similar to the one below that I need to process and display in a windows fomm using a combination of tree-view and list view controls in C#.  The element <Company> is the only one that I can guarantee will come in the same standard format each time the XML is created and I need to display that separately on the windows form.  The rest of the elements can change and could possibly more nested elements within them.  What is the best way to loop through such an XML document and display the contents?  I am a novice XML user so any help will be appreciated.  Also, what is the determining factor in using the XML document object Vs XmlTextReader?  Thanks.


"test.xml"

<?xml version="1.0" encoding="utf-8"?>
<Company>
<name>x</name>
<address>y</address>
<sector>z</sector>
</Company>
<Departments>
<Sales>
<President>
<First Name>Peter</First Name>
<Last Name>Pan</Last Name>
<Age>45</Age>
</President>
<VP>
<First Name>John</First Name>
<Last Name>Doe</Last Name>
<Age>35</Age>
</VP>
<Sales>
</Departments>
 
0
Comment
Question by:fifo123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10752589
The best thing for you is to use the Xml namespace. In the beginning of your file include:

using System.Xml;

Then you can use the power of the XmlDocument and XmlNode classes. This is a huge area, so I will just tell you that you can use for example the Load method of the XmlDocument to load the whole document.

You can start your investigation of the Xml namespace from here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmldocumentclasstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlnodeclasstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxml.asp
0
 

Author Comment

by:fifo123
ID: 10763026
Ok,
So, I kinda figured out how to recursively traverse through all the elements in the given XML.  Now, I'm wanting to display that using a treeview control.  The code below works fine but it displays the results as follows:

+Name
+Address
+Sector
+Departments
  +Sales
    +President
      +FirstName
      +LastName
      +Age

If I expand the Name node, I get the value " = 'x' " which is fine.  To see the value of the FirstName for the node President, I would have to expand that node.  How would I make it such that it will display:

Name = 'X'
Address = 'Y'
Sector = 'z'
+ President
  - FirstName = "John"
  - LastName = "Doe"


The XML document looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Company>
<name>x</name>
<address>y</address>
<sector>z</sector>
<Departments>
<Sales>
<President>
<FirstName>Peter</FirstName>
<LastName>Pan</LastName>
<Age>45</Age>
</President>
<VP>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Age>35</Age>
</VP>
</Sales>
</Departments>
</Company>

private void frmTest_Load(object sender, System.EventArgs e)
{
XmlDocument Doc = new XmlDocument();
Doc.Load(@"C:\test.xml");
XmlNodeList nodeList = Doc.SelectNodes("/Company");
foreach(XmlNode oNode in nodeList)
{
    DisplayNode(oNode, tview.Nodes);      
}
}

private void DisplayNode(XmlNode i_Node, TreeNodeCollection t_Nodes)
{
    string Text = "";
    foreach (XmlNode node in i_Node.ChildNodes)
    {                              
       if(node.Value != null)
            Text = " = '" + node.Value + "'";                              
       else if(node.Attributes != null && node.Attributes.Count > 0)
            Text = node.Attributes[0].Value;
       else
             strText = node.Name;

       TreeNode myNode = new TreeNode(strText);
       t_Nodes.Add(myNode);
       ProcessNode(node, myNode.Nodes);
}
}
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 50 total points
ID: 10763481
0
 

Author Comment

by:fifo123
ID: 10768339
TheAvenger..... Thanks for the tip.

I tried the sample from the microsoft site.  I used my XML document and it pretty much does what my code above does.  What I'm attempting to do is that if a node doesn't have any child nodes, display its name followed by its value.  For example:

for the element:

<name>x</name>, I want to display name='x'

and for the nested elements:

<Departments>
<Sales>
<President>
<FirstName>Peter</FirstName>
<LastName>Pan</LastName>
</President>
</Sales>
</Departments>

I want to display
-Departments
....-Sales
......-President
..........FirstName = 'Peter'
..........LastName = 'Pan'

What my code and the sample from microsoft is doing the following:
-Departments
.....-Sales
.......-President
.........-FirstName
.............Peter
.........-LastName
.............Pan

I've been trying for the last day and half, but no success yet.  Any suggestions?

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10769134
have you tried something like:

XmlNode node = ....;  // Find the node or go over them recursively

if (node.ChildNodes.Count == 0)
  tree.Add (node.Value);

I am not sure the names of all these properties are correct, but this is the idea and you can find more on the link I gave you.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

752 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