Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I am Searchin for a code the should fetch values from XML according to the filter value i give.

For example i have the following XML file
<Customers>
    <Customersinfo>
        <CustomerID>1</CustomerID>
        <CustomerName>AAA</CustomerName>
        <CustomerAge>20</CustomerAge>
    </Customersinfo>
    <Customersinfo>
        <CustomerID>2</CustomerID>
        <CustomerName>BBB</CustomerName>
        <CustomerAge>25</CustomerAge>
    </Customersinfo>
    <Customersinfo>
        <CustomerID>3</CustomerID>
        <CustomerName>CCC</CustomerName>
        <CustomerAge>23</CustomerAge>
    </Customersinfo>
</Customers>


I want to display only the details of Customer "1" alone in VB.NET, now the data should be binded with Datagridview control.

Please help me in this. i am in very much need of this. i tried the following code to print the values in Label but it is not working for me.

Dim doc = New System.Xml.XmlDocument()
doc.Load("D:\\Test\\Customers.xml")
Dim root = doc.DocumentElement
Dim lst = root.GetElementsByTagName("CustomerID")
Dim n As System.Xml.XmlNode
For Each n In lst
While (n.InnerText = "1")
Label1.Text = Label1.Text + " " + n.InnerText '// print id text
End While
Next


Can you please help me to find the solution.

Thanks
Posted

Well, you could try using the debugger. When you ask for help, you could try telling us what 'not working' means. You could just think about your code, and perhaps read a basic programming book.

Imranfints wrote:
Dim lst = root.GetElementsByTagName("CustomerID")


You should just use SelectNodes on the document itself.


Imranfints wrote:
For Each n In lstWhile (n.InnerText = "1")Label1.Text = Label1.Text + " " + n.InnerText '// print id textEnd While


This is insanity. Why not just use SelectNodes to get the nodes you want ? Why is a while loop the right choice here ? Where did you think you were selecting the details of the record apart from the Id ? I don't see where there's any code that looks like you even tried to do this. It looks to me like you took a wild guess, and once it compiled, figured we could tell you how to fix it. It needs to be rewritten, preferally by someone who knows how XML works.
 
Share this answer
 
To answer your question, the problem with your code lies with the code While (n.InnerText = "1") ... End While. You are basically creating an infinite loop with your While statement, since the first node that meets your condition (n.InnerText = "1") will cause the While statement to loop forever.


Change this code...
While (n.InnerText = "1")
    Label1.Text = Label1.Text + " " + n.InnerText '// print id text
End While


to this...
If n.InnerText = "1" Then
    Label1.Text = Label1.Text + " " + n.InnerText '// print id text
End If


That said, there are better ways to do this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900