Solved

Looping through an XML document

Posted on 2004-04-04
7
283 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
  • 3
  • 2
7 Comments
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:fifo123
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

10 Experts available now in Live!

Get 1:1 Help Now