I have part of XmlDocument Example.xml as given below:
<rapaine dotoc='palin' domap='rattmin'> <derif meet='local' /> <derif meet='intro' /> . . . </rapaine>
Here i am creating a Nodelist and fetching the element raplin to get its attributes.
Now i want to make sure that whether the attributes ‘dotoc’ and ‘domap’ are the attributes of rapaine with respective values which are always fixed.Then only i can access the childNodes ‘deriff’ with its attribute ‘meet’. here the value only changes.
i have written a part of code there are no compile errors but on debugging i found that its not going inside the for loop to check its attributes and child nodes.
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); } } } } } } } } }
Can you please tell me where i am going wrong.. Thanks….
You can do this with the following code using XPath. Since XPath is a language designed specifically to query XML documents, you should consider learning it. Most newbies like to start learning at W3schools.
Here is the code:
For your reference, the XPath expression:
can be explained as:
a) Find all
rapaineroot level elements that have an attributedotocwith a value of ‘palin’ and also have an attributedomapwith a value ‘rattmin’.b) Within those
rapaineelements, find allderifchildren.c) Within those
derifelements, retrieve allmeetattributes.Note how much more concise the code becomes.