Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Looping through an XML document

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
fifo123
Asked:
fifo123
  • 3
  • 2
1 Solution
 
TheAvengerCommented:
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
 
fifo123Author Commented:
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
 
fifo123Author Commented:
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
 
TheAvengerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now