views:

622

answers:

3

Hi Everyone, 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....

A: 

Can't you just fix this with a simple XPath expression?

All the nested loops with condition is simply asking for trouble.

Gerrie Schenck
No i cant use XPath.To be honest i dont know how to use XPath here.Thanks
crazy_itgal
yes you can use XPath for this, but you'll have to look into it.
Natrium
I'm not saying your entire problem will be solved with one expression, you might want to break it up into smaller pieces, and there might be some iteration necessary to do it, but in general I'm sure it is possible.
Gerrie Schenck
A: 

depending on what version of .NET you use, LINQ might be able to simplify this.

Natrium
A: 

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:

XmlNodeList meetList = opfXmlDoc.SelectNodes("/rapaine[(@dotoc = 'palin') and (@domap = 'rattmin')]/derif/@meet")
if (meetList.Count > 0)
{
  foreach (XmlNode meet in meetList)
  {
    spineListOfSmilFiles.Add(meet.Value);
  }
}

For your reference, the XPath expression:

/rapaine[(@dotoc = 'palin') and (@domap = 'rattmin')]/derif/@meet

can be explained as:

a) Find all rapaine root level elements that have an attribute dotoc with a value of "palin" and also have an attribute domap with a value "rattmin".

b) Within those rapaine elements, find all derif children.

c) Within those derif elements, retrieve all meet attributes.

Note how much more concise the code becomes.

Cerebrus
Oh that Luks great.Thanks for the Link and the part of code.
crazy_itgal
My pleasure! Happy learning. :-)
Cerebrus