I am facing a trouble for a couple of days. I have an XML string which is retrieved from a web service. I want to list them into my webform.
My (very very simplified) XML string looks like:
<school>My School</school>
<department>Logistics</department>
<department>Trading</department>
<department>Food Control and Quality Analysis</department>
<school>Her School</school>
<department>Poultry</department>
<department>Forestry</department>
I use the following code to create a treeview:
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(service.getTree());
var SchoolNames = xdoc.XPathSelectElements("//school");
foreach (XElement sn in SchoolNames)
{
divContent.InnerHtml += string.Format("<h4>{0}</h4>", sn.Value);
var Departments = sn.XPathSelectElements("//department");
foreach (XElement d in Departments)
{
divContent.InnerHtml += string.Format("<h5>{0}</h5>", d.Value);
}
}
I get the following result:
<h4>My School</h4>
<h5>Logistics</h5>
<h5>Trading</h5>
<h5>Food Control and Quality Analysis</h5>
<h5>Poultry</h5>
<h5>Forestry</h5>
<h4>Her School</h4>
<h5>Logistics</h5>
<h5>Trading</h5>
<h5>Food Control and Quality Analysis</h5>
<h5>Poultry</h5>
<h5>Forestry</h5>
which I should get
<h4>My School</h4>
<h5>Logistics</h5>
<h5>Trading</h5>
<h5>Food Control and Quality Analysis</h5>
<h4>Her School</h4>
<h5>Poultry</h5>
<h5>Forestry</h5>
In my opinion, the problem is on the line
var Departments = sn.XPathSelectElements("//department");
If a filtering criteria is written here then the problem will vanish out. If one can write here a "only departments of which ancestor is the current d" condition. But I do not know how to write it.
No comments:
Post a Comment