Need help on looping throug a xml file to find a specific innerxml

  • Thread starter Thread starter janhm
  • Start date Start date
J

janhm

Hello.

I need to loop through a xml file finding a specific innerxml text and then
ad the content to a treeview.

For example. If the innerxml im searching for is "item001" then I want the
content in <items></items>

added to my treeview.

<items>
<item>item001</item>
<more_item>stuff</more_item>
</items>

Any ideas how to do that.

I must admit I have little xml and treeview experience so I hope you are
able to help or guide me to an examble.

Thanks

//Janhm
 
janhm said:
Hello.

I need to loop through a xml file finding a specific innerxml text and then
ad the content to a treeview.

Use an XPATH query. Look up the SelectNodes method of the
XmlDataDocument class.
 
It appears that XPATH isn't supported, as im developing for a smart device..

any other ideas ?


//Jan
 
XmlReader r = new XmlTextReader("myfile.xml");

while(r.Read())
{
r.MoveToContent(); // move past whitespace, comments, etc
if( r.NodeType == XmlNodeType.Element && r.LocalName== <nodename> && r.NamespaceUri==<namespace>)
{
r.Read(); // move to the text node of the required element
MessageBox.Show(r.Value); // get the text node value
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

It appears that XPATH isn't supported, as im developing for a smart device..

any other ideas ?


//Jan
Use an XPATH query. Look up the SelectNodes method of the XmlDataDocument
class.



[microsoft.public.dotnet.languages.csharp]
 
It appears that XPATH isn't supported, as im developing for a smart device..

That's a bit rubbish.

I was once involved in an eVB project, and I'm pretty sure we used MSXML
to evaluate XPATH expressions on a PDA. I wrote the synchronisation
software in C#, but I think I remember helping the eVB guy with XPATH
syntax.

You might be able to use MSXML via COM interop, but I think it's
probably simpler just to iterate the nodes and examine them.
 
Of course - that would require COM interop to be supported ;-)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

That's a bit rubbish.

I was once involved in an eVB project, and I'm pretty sure we used MSXML
to evaluate XPATH expressions on a PDA. I wrote the synchronisation
software in C#, but I think I remember helping the eVB guy with XPATH
syntax.

You might be able to use MSXML via COM interop, but I think it's
probably simpler just to iterate the nodes and examine them.
 
and a friend told me when developing for smartdevice in vs.net I was using
compact framework. But as I said I don't know..

You are, and those XPATH methods aren't supported. I think you'll have
to resort to brute force and ignorance.
 
When you say you want a specific innerxml what exactly do you want to do with it?

Do you want to return the xml fragment?
Do you want to process this section internally?
Do you want to return a value from this fragment?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hmm, I don't know so much about this. (comming from delphi programming)..

I got an error and goggled for it.. found this one :

http://groups.google.dk/groups?hl=d...WhS6MlYDHA.3444%40tk2msftngp13.phx.gbl&rnum=1

and a friend told me when developing for smartdevice in vs.net I was using
compact framework. But as I said I don't know..

//Jan
 
When I enter KIOSK001 in my textbox i want to search through the following
xml and look up <kiosknavn>:

<kiosk>
<kiosknavn>KIOSK001</kiosknavn>
<serienummer>099901500001</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
<adresse>adresse</adresse>
<postnummer>postnummer</postnummer>
<by>by</by>
<kontakt_tlf>00000000</kontakt_tlf>
</kiosk>
<kiosk>
<kiosknavn>KIOSK002</kiosknavn>
<serienummer>099901200002</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
.....
</kiosk>

Then I want it to display the entire section between <kiosk>and </kiosk> in
my treeview. but only the section that has the correct <kiosknavn>

hope this makes sense... :)

//Jan
 
How big is the document?

XmlReader is forward only. so you can't get to the kiosknavn element to find the correct one and then back up to the Kiosk element. So the best thing to do is to loop through till you find the kiosk element, extract the subtree then load the subtree into another XmlReader and check if its the correct one, if it is return it otherwise move on to the next one.

while(r.Read())
{
if( r.NodeType == XmlNodeType.Element && r.LocalName == "kiosk")
{
string s = r.GetOuterXml();
// etc.
}
}

Regards

RIchard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

When I enter KIOSK001 in my textbox i want to search through the following
xml and look up <kiosknavn>:

<kiosk>
<kiosknavn>KIOSK001</kiosknavn>
<serienummer>099901500001</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
<adresse>adresse</adresse>
<postnummer>postnummer</postnummer>
<by>by</by>
<kontakt_tlf>00000000</kontakt_tlf>
</kiosk>
<kiosk>
<kiosknavn>KIOSK002</kiosknavn>
<serienummer>099901200002</serienummer>
<kontaktperson>navn efternavn</kontaktperson>
....
</kiosk>

Then I want it to display the entire section between <kiosk>and </kiosk> in
my treeview. but only the section that has the correct <kiosknavn>

hope this makes sense... :)

//Jan
When you say you want a specific innerxml what exactly do you want to do
with it?

Do you want to return the xml fragment?
Do you want to process this section internally?
Do you want to return a value from this fragment?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk



[microsoft.public.dotnet.languages.csharp]
 
Its not so big yet. is there a size limit ?

I tried the solution below, but got a

'System.Xml.XmlReader' does not contain a definition for 'GetOuterXml'


//Jan
 
Sorry that should have been ReadOuterXml() (you could try intellisense ;-) )

if its not big you could load it into an XmlDocument (although I wouldn't normally suggest that on a small device because of the memory overhead and speed). You can traverse backwards in an XmlDocument so you can use the GetElementsByName method as follows:

XmlDocument d = new XmlDocument();
d.Load(<file name>);
XmlNodeList nodes = d.GetElementsByTagName("kiosk");
foreach(XmlNode node in nodes)
{
if( node.FirstChild.FirstChild.Value == <text box value>)
{
return node.OuterXml;
}
}

However, the XmlReader versioj is more efficient

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Its not so big yet. is there a size limit ?

I tried the solution below, but got a

'System.Xml.XmlReader' does not contain a definition for 'GetOuterXml'
 
ahh yes...ReadOuterXml() worked //been a long day :)

I will look at the other option also,. see what works the best for me...

thanks to both of you.

//Jan
 
Back
Top