I am trying to parse TOC.ncx (which is xml) in WP7 C#
Following is the content of file:
<navMap>
<navPoint id="navpoint-1" playOrder="1"><navLabel><text>Cover</text></navLabel><content src="xhtml/cover.html" /></navPoint>
<navPoint id="navpoint-2" playOrder="2"><navLabel><text>Title Page</text></navLabel><content src="xhtml/title.html" /></navPoint>
<navPoint id="navpoint-3" playOrder="3"><navLabel><text>Contents</text></navLabel><content src="xhtml/toc.html" /></navPoint>
<navPoint id="navpoint-4" playOrder="4"><navLabel><text>Foreword</text></navLabel><content src="xhtml/foreword.html" /></navPoint>
<navPoint id="navpoint-5" playOrder="5"><navLabel><text>Preface</text></navLabel><content src="xhtml/preface.html" /></navPoint>
<navPoint id="navpoint-6" playOrder="6"><navLabel><text>Introduction</text></navLabel><content src="xhtml/introduction.html" /></navPoint>
<navPoint id="navpoint-7" playOrder="7"><navLabel><text>The East India Company</text></navLabel><content src="xhtml/chapter001.html" />
<navPoint id="navpoint-8" playOrder="8"><navLabel><text>The Voyages</text></navLabel><content src="xhtml/chapter001.html#eas0000222" /></navPoint>
<navPoint id="navpoint-9" playOrder="9"><navLabel><text>Mission to India</text></navLabel><content src="xhtml/chapter002.html" /></navPoint>
<navPoint id="navpoint-10" playOrder="10"><navLabel><text>Madras, Bombay and Calcutta</text></navLabel><content src="xhtml/chapter003.html" /></navPoint>
<navPoint id="navpoint-11" playOrder="11"><navLabel><text>Growth Amidst Turmoil</text></navLabel><content src="xhtml/chapter004.html" /></navPoint>
<navPoint id="navpoint-12" playOrder="12"><navLabel><text>A Bridge between Many Worlds</text></navLabel><content src="xhtml/chapter005.html" /></navPoint>
<navPoint id="navpoint-13" playOrder="13"><navLabel><text>Partners and Agents</text></navLabel><content src="xhtml/chapter006.html" /></navPoint>
<navPoint id="navpoint-14" playOrder="14"><navLabel><text>War and Plunder</text></navLabel><content src="xhtml/chapter007.html" /></navPoint>
<navPoint id="navpoint-15" playOrder="15"><navLabel><text>Ruler in India</text></navLabel><content src="xhtml/chapter008.html" /></navPoint>
<navPoint id="navpoint-16" playOrder="16"><navLabel><text>The Company and Indian History</text></navLabel><content src="xhtml/chapter009.html" /></navPoint>
</navPoint>
<navPoint id="navpoint-17" playOrder="17"><navLabel><text>Timeline</text></navLabel><content src="xhtml/appendix001.html" /></navPoint>
<navPoint id="navpoint-18" playOrder="18"><navLabel><text>Bibliography</text></navLabel><content src="xhtml/appendix002.html" /></navPoint>
<navPoint id="navpoint-19" playOrder="19"><navLabel><text>Copyright Page</text></navLabel><content src="xhtml/copyright.html" /></navPoint>
</navMap>
And following is the code I have written to get the navPoint:
private List<NavPoint> GetNavigationChildren(IEnumerable<XElement> elements, XNamespace nameSpace)
{
List<NavPoint> navigationPoints = new List<NavPoint>(elements.Count());
if (!elements.Any())
return navigationPoints;
navigationPoints.AddRange(elements.Select(navPoint =>
new NavPoint(navPoint.Attribute("id").Value,
navPoint.Element(nameSpace + "navLabel").Element(nameSpace + "text").Value,
HttpUtility.UrlDecode(navPoint.Element(nameSpace + "content").Attribute("src").Value),
int.Parse(navPoint.Attribute("playOrder").Value), Content[0] as EpubContent,
GetNavigationChildren(navPoint.Elements(nameSpace + "navPoint"),
nameSpace))));
return navigationPoints;
}
By executing above code, I am able to get the top level nav points(i.e. navPoint-1 to navPoint-7 and then navPoint-17 to navPoint-19) but the problem is its not getting the child elements i.e. navPoints of navPoint-7.
Can anyone suggest what wrong I am doing here??
Calling of the GetNavigationChildren is done as:
XElement xElement = XElement.Parse(@xmlText);
XNamespace xNamespace = xElement.Attribute("xmlns") != null ? xElement.Attribute("xmlns").Value : XNamespace.None;
List<NavPoint> navPoints = GetNavigationChildren(xElement.Element(xNamespace + "navMap").Elements(xNamespace + "navPoint"), xNamespace);
And the class def of NavPoint is:
public NavPoint(string id, string title, string source, int order, EpubContent contentData, List<NavPoint> children)
{
ID = id;
Title = title;
Source = source;
Order = order;
ContentData = contentData;
Children = children;
}
Please help!
If you need a flat list of NavPoints, you need to use
Descendantsinstead ofElements.