Parse tree-shaped xml with XmlReader

  • Thread starter Thread starter Sin Jeong-hun
  • Start date Start date
S

Sin Jeong-hun

The xml's structure is like the following
<Directory>
<Directory>
<Directory>
<Directory>
</Directory>
</Directory>
<Directory>
<Directory/>
</Directory>
<Directory>
</Directory>
</Directory>
<Directory>
</Directory>
Yes, a very simple structure. Directory can contain other Directories
be empty. I would like to parse this xml. Say a class like this;
class Directory
{
public Directory SubDirectory;
public void AddSubDirectory(Directory);
}
But I'm confused with XmlReader. Could you please give me any simple
code snippet or psudo code for this task? Or any tutorial web page for
similar tasks.Thank you.
 
I made some typoes. So I correct my post.
======================================================
The xml's structure is like the following
<Directory>
<Directory>
<Directory>
<Directory/>
</Directory>
</Directory>
<Directory>
<Directory/>
</Directory>
<Directory>
</Directory>
</Directory>
<Directory>
</Directory>
Yes, a very simple structure. Directory can contain other Directories
or can
be empty. I would like to parse this xml into a class. Say a class like
this;
class Directory
{
Directory[] SubDirectories;
public void AddSubDirectory(Directory);
}

But I'm confused with XmlReader. Could you please give me any simple
code snippet or psudo code for this task? Or any tutorial web page for
similar tasks.Thank you.
 
Here's some code I was just working on:

string xmlTarget = getPath() + @"\focus.xml";
XmlTextReader reader = new XmlTextReader (xmlTarget);


reader.MoveToFirstAttribute();
reader.MoveToContent();

while (reader.Read())
{

if (reader.Name.Equals("name")
&& reader.NodeType == XmlNodeType.Element)
{
sight += reader.GetAttribute("restart")+"|"; //retina 0
sight += reader.GetAttribute("subject")+"|"; //retina 1
sight += reader.GetAttribute("appserver")+"|"; //retina 2
sight += reader.ReadElementString("name") + "|"; //retina 3
//Debug.WriteLine(sight);
}
}
 
Back
Top