Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : Help needed with XPathNav



jainamber NO[at]SPAM gmail.com
12/19/2005 1:29:14 PM
Hi,
I am using this code to search on the id attribute of the
Report element in this XML fragment. If I find a match for the id, then
I need to return the value of the attributes of the child column for
each column.
The code I have written is not working and I do not have a clue about
what to do next.

Here is the XML document.

<?xml version="1.0"?>
<Reports>
<Report id="testReport">
<SP id="DSMM_TestReport" />
<Columns>
<Column id="Id" visible="false" />
<Column id="Description" visible="true" />
</Columns>
</Report>
<Report id="testReport1">
<SP id="DSMM_TestReport1" />
<Columns>
<Column id="Id1" visible="false" />
<Column id="Description1" visible="true" />
</Columns>
</Report>
</Reports>



Here is the code that I am using:

Dim xPathDoc As New XPathDocument(FileName)
Dim xPathNav As XPathNavigator = xPathDoc.CreateNavigator()
Dim xPathItr As XPathNodeIterator
xPathItr =
xPathNav.Select("Reports[Report[@id='testReport']]/@id")
Dim str As String
Do While xPathItr.MoveNext
str = str & xPathItr.Current.Value
Loop


If you know of any other way using XpathNav to do the same, please let
me know.

Any help is greatly appreciated.

Thanks,

Amber
swapna guddanti [MSFT]
12/19/2005 4:19:50 PM
hi,
the following code should help. It gets a nodeset of the Report nodes with
the matching id and then iterates over each Report node and gets the nodeset
of all the attributes under the Columns node for that Report node and
concatenates them
Function test3()
Dim xPathDoc As New XPathDocument(FileName)
Dim xPathNav As XPathNavigator = xPathDoc.CreateNavigator()
Dim xPathItr As XPathNodeIterator
xPathItr = xPathNav.Select("Reports/Report[@id='testReport']")
Dim str As String
Do While xPathItr.MoveNext
Dim xPathNavTemp As XPathNavigator
xPathNavTemp = xPathItr.Current
Dim xPathItrTemp As XPathNodeIterator
xPathItrTemp = xPathNavTemp.Select("./Columns/Column/@*")
Do While xPathItrTemp.MoveNext
str = str & xPathItrTemp.Current.Value
Loop
Loop
Console.WriteLine(str)
End Function


hope this helps,
swapna

[quoted text, click to view]

Oleg Tkachenko [MVP]
12/20/2005 11:54:54 AM
[quoted text, click to view]

/Reports/Report[@id='testReport']/Columns/Column/@id

--
Oleg Tkachenko [XML MVP, MCAD]
AddThis Social Bookmark Button