tags:

views:

112

answers:

5

The following code gives me a nodeList to itterate over:

XPathNavigator thisNavigator = thisXmlDoc.CreateNavigator();
XPathNodeIterator dossierNodes = thisNavigator.Select("changedthisname/dossiers/dossier");

I am processing this nodeList, and i need to grab another nodelist out of this list. I am trying to do this by using this code:

XPathNavigator alineanodesNavigator = dossierNodes.Current;
XPathNodeIterator alineaNodes = alineanodesNavigator.Select("/dossier/alineas/alinea");

I am using this code inside the while(dossierNodes.MoveNext()) loop and want this nodelist to be filled up with all "allinea's". However i am not getting any results back to my alineaNodes iterator.

The document structure is like this:

alt text

How to get the alinea nodes from the current dossier node??

I debugged and this came out:

alt text

    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
    string xml = reader.ReadToEnd();
    XmlDocument thisXmlDoc = new XmlDocument();
    thisXmlDoc.LoadXml(xml);

    XPathNavigator thisNavigator = thisXmlDoc.CreateNavigator();
+2  A: 

You are already in the dossier node so when you put in a '/' in the beginning of the XPath query you are saying "From the parent node", which happens to already be the dossier node at this point. Change your XPath query to:

EDIT:

After seeing your output, I realize my mistake. You should just drop the leading forward slash from your original query.

"dossier/alineas/alinea"

And that should do the trick.

Josh
I have no clue why, but this doesn't do the trick. It says this doesn't give back any results.
Younes
@Josh and @Younes: check my answer for correct relative path.
Alejandro
A: 

Linq to XML is perfect for this instance.

var alineaNodes = from alinea in XDocument.Load(alldata.xml).Descendents("alinea")
                  select alinea;

that returns an IEnumerable with all elements in the alldata.xml. Hope this helps. And really look into Linq it is great for these things.

Adkins
I don't want a list with all alineaNodes as i am in the itterator working with the current item. I need to add the alineas to my current working list.
Younes
You can change XDocument.Load(alldata.xml) to be any list, XML document, or database that you want. You can then adjust the part that says Descendents("aliena") to whatever method is needed from your list so that you can look at the individual elements contained within. Linq really does have all the functionality that it seems you are looking for in this case. You can also add conditionals to it (e.g. adding a where statement before the select).
Adkins
Okay thanks, i will go and try it uit quickly. If it works you'll get the points ;).
Younes
I put it at the bottom of my post. (how to edit my code to make it work like yours please)
Younes
A: 

Okay, i have solved this one.

Instead of trying to get the alinea's by using xpath i'll just use the SelectDescendants on my node navigator like what here:

            XPathNavigator alineanodesNavigator = dossierNodes.Current;
            XPathNodeIterator alineaNodes = alineanodesNavigator.SelectDescendants("alinea", "", false);


            List<Alinea> thisAlineaList = new List<Alinea>();
Younes
A: 

In your last call to Select(), use the following XPath expression: "//alinea".

The following code snippet iterates over alineas:

        XPathNavigator thisNavigator = doc.CreateNavigator();
        XPathNodeIterator dossierNodes = thisNavigator.Select("//dossier");
        while (dossierNodes.MoveNext())
        {
            XPathNavigator alineanodesNavigator = dossierNodes.Current;
            XPathNodeIterator alineaNodes = alineanodesNavigator.Select("//alinea");

            while (alineaNodes.MoveNext())
            {

            }
        }
louisgab
Yes that is what i've done ;).
Younes
+2  A: 

The right full path is

changedthisname/dossiers/dossier/alineas/alinea

So, if you are selecting changedthisname/dossiers/dossier first, then the relative path is:

alineas/alinea
Alejandro
Yes, i seriously have tried that but it wouldn't work at all. I have even debugged all this and i have no clue why this wouldn't work. However i've already solved this issue now :). Thanks for helping.
Younes
+1 for the only correct answer.
Dimitre Novatchev
Answer was according to me also correct, but somehow I was not able to get the list with this expression ;). Anyway u are right and I have tried this one. U got the points.
Younes