0

我正在尝试在 WP7 C# 中解析 TOC.ncx(即 xml)

以下是文件内容:

<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>

以下是我编写的用于获取的代码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;
        }

通过执行上面的代码,我可以获得顶级导航点(即 navPoint-1 到 navPoint-7,然后 navPoint-17 到 navPoint-19),但问题是它没有获取子元素,即 navPoint-7 的 navPoints .

谁能建议我在这里做错了什么?

的调用GetNavigationChildren是这样完成的:

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);

而类 defNavPoint是:

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;
        }

请帮忙!

4

1 回答 1

1

如果您需要导航点的平面列表,则需要使用Descendants而不是Elements.

于 2012-09-11T19:54:59.950 回答