通过过滤读取XML内容 [英] Read XML Contents by Filtering

查看:55
本文介绍了通过过滤读取XML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI 我正在搜索代码,应根据我提供的过滤器值从XML提取值.

例如,我有以下XML文件

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>



我只想在VB.NET中仅显示客户"1"的详细信息,现在该数据应与Datagridview控件绑定.

请帮助我.我非常需要这个.我尝试了以下代码来打印Label中的值,但它对我不起作用.



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

推荐答案

好,您可以尝试使用调试器.当您寻求帮助时,您可以尝试告诉我们无效"的含义.您可以考虑一下您的代码,甚至可以阅读一本基本的编程书.

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写道:
Imranfints wrote:

Dim lst = root.GetElementsByTagName("CustomerID")

Dim lst = root.GetElementsByTagName("CustomerID")



您应该只在文档本身上使用SelectNodes.




You should just use SelectNodes on the document itself.


Imranfints写道:
Imranfints wrote:

在lstWhile中,每个n(n.InnerText ="1")Label1.Text = Label1.Text +" + n. InnerText''//打印id textEnd While

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



这是精神错乱.为什么不只使用SelectNodes来获取所需的节点?为什么while循环在这里是正确的选择?您认为您在哪里选择ID以外的记录详细信息?我看不到有什么代码看起来像您甚至尝试执行此操作.在我看来,您似乎经过了疯狂的猜测,并且一旦编译完成,我们便可以告诉您如何解决它.需要重写它,最好由知道XML的工作原理的人来重写.



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.


要回答您的问题,代码的问题在于代码While (n.InnerText = "1") ... End While.基本上,您正在使用While语句创建无限循环,因为满足条件的第一个节点(n.InnerText = "1")将导致While语句永远循环.


更改此代码...
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.


这篇关于通过过滤读取XML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆