read specific xml values

Posted on 2011-10-31
Last Modified: 2012-05-12

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\ 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

Question by:SimonPrice33
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Can you use Linq?

    Author Comment

    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...
    LVL 28

    Expert Comment

    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.


    Author Comment

    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...

    LVL 74

    Accepted Solution

    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

    LVL 7

    Expert Comment

    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

    Author Closing Comment

    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

    Author Comment


    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?
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Use <br />, not </br>. Web pages naturally consolidate multiple whitespaces, so newlines don't work (I think they do in a <pre> tag).
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    I should say "browsers" naturally consolidate multiple whitespaces, not web pages.

    Author Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now