我的XmlDocument的example.xml的一部分,如下所示:从XmlDocument的NodeList中获取属性?
<rapaine dotoc="palin" domap="rattmin">
<derif meet="local" />
<derif meet="intro" />
.
.
.
</rapaine>
这里我创建一个节点列表和打水元素raplin得到它的属性。
现在我想确保属性'dotoc'和'domap'是否是rapaine的属性,各个值总是固定的。然后,只有我可以通过属性'meet'访问childNodes的'deriff' 。这里的价值只是变化。
我已经写了一部分代码没有编译错误,但在调试时发现它没有进入for循环来检查它的属性和子节点。
XmlNodeList listOfSpineRootNodes = opfXmlDoc.GetElementsByTagName("rapine");
for (int x = 0; x < listOfSpineRootNodes.Count; x++)
{
XmlAttributeCollection spineAttributes = listOfSpineRootNodes[x].Attributes;
string id = spineAttributes[0].Value;
if (spineAttributes != null)
{
XmlNode attrToc = spineAttributes.GetNamedItem("dotoc");
XmlNode attrPageMap = spineAttributes.GetNamedItem("domap");
if (attrToc.Value == "palin" && attrPageMap.Value == "rattmine")
{
if (listOfSpineRootNodes != null)
{
foreach (XmlNode spineNodeRoot in listOfSpineRootNodes)
{
XmlNodeList listOfSpineItemNodes = spineNodeRoot.ChildNodes;
if (listOfSpineItemNodes != null)
{
foreach (XmlNode spineItemNode in listOfSpineItemNodes)
{
if (spineItemNode.NodeType == XmlNodeType.Element
&& spineItemNode.Name == "derif")
{
XmlAttributeCollection spineItemAttributes = spineItemNode.Attributes;
if (spineItemAttributes != null)
{
XmlNode attrIdRef = spineItemAttributes.GetNamedItem("meet");
if (attrIdRef != null)
{
spineListOfSmilFiles.Add(attrIdRef.Value);
}
}
}
}
}
}
}
}
}
能否请你告诉我,我错了.. 感谢....
不,我不能使用XPath。说实话,我不知道如何在这里使用XPath。 谢谢 – 2009-04-24 08:01:50
是的,你可以使用XPath来做这件事,但你必须仔细研究它。 – Natrium 2009-04-24 08:15:38