• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

read specific xml values


As usual, my user requirements have changed at the point of delivery...  I have written an app that will push out data to XML and a webpage that will read all the vlaues using this code and a data grid
(snippet ID 8230700)
 but now my user wants to be able to search the XML for specific records from the Stub Value
(snippet ID 8230701)

I am writing in asp.net\vb.net 2.0  and would be grateful for any help.

There will be a minimum of two records per stub value and will need to show all records matching that specific stub value.

Any help will be gratefully received.  

Dim ds As New DataSet()
        If File.Exists("\\wintdc02\PUBLIC\TarehouseXML\" & machine.Text & year.Text & month.Text & day.Text & ".xml") Then
            ds.ReadXml("\\wintdc02\PUBLIC\TarehouseXML\" & machine.Text & year.Text & month.Text & day.Text & ".xml")
            datagrid1.DataSource = ds
            Response.Write("The file you are looking for does not exist or has been moved")
        End If

Open in new window

- <sample>
   <SugarValue /> 
   <AminoValue /> 
   <SodiumValue /> 
   <PotasiumValue /> 

Open in new window

1 Solution
käµfm³d 👽Commented:
Can you use Linq?
SimonPrice33Author Commented:
I have just moved my project into a .Net 3.5 Framework so should be able to now...  but still not entierly sure on how to do it...
Do you mean that the user wants to be able to search through all existing xml files for a given value of "stub"?

Because that is going to be slow, opening each xml file and search for a given value. Better store everything in a database and search.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

SimonPrice33Author Commented:
Hy Sybe,

Yes thats what they would like to do...   Speed wont be too much of an issue because they will be pulling the files from a server local to that site and there is a new file for each day so it wont be cycling through thousands upon thousands of records.

The most we will be talking about is 400 records at a worst case scenario. I agree a database would be the better way forward but we are just managing to get them off of a line printer so this is a huge step in the right direction...

käµfm³d 👽Commented:
Using the XML you posted, you could do something like this:

Module Module1

    Sub Main()
        Dim records = From record In XDocument.Load("input.xml") _
                                              .Descendants("sample") _
                      Where record.<Stub>.Value = "99999999A" _
                      Select record

        For Each record In records

    End Sub

End Module

Open in new window

where "records" would be the equivalent to your "ds". You can assign the query directly to the data source also:

datagrid1.DataSource = records

Open in new window

you can get the rows of the datatable in the dataset by using select. =)

Dim Rows() As DataRow = ds.tables(0).Select("[Stub]='" & string_SearchValue & "'")

if rows.length >0 then
'xml contains stub value
'xml does not contain stub value
SimonPrice33Author Commented:
This worked a charm, although datagrid1.DataSource = records didnt present how I would have expected it to...

but you have given me enough to work with for now.

Thank you
SimonPrice33Author Commented:

I am currently testing with 3 records with the same stub value but all are returning on the same line in my aspx page.

i have tried system.environment.newline, </br>, vbcr, vbcrlf, chr(13), vbnewline to try and get the new record onto the next line, however none of these work.

Do you have any suggestions?
käµfm³d 👽Commented:
Use <br />, not </br>. Web pages naturally consolidate multiple whitespaces, so newlines don't work (I think they do in a <pre> tag).
käµfm³d 👽Commented:
I should say "browsers" naturally consolidate multiple whitespaces, not web pages.
SimonPrice33Author Commented:
ive got it sorted now thanks, have written the response.write into a table that shows the data how I would like it.

Thank you very much for your help

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now