从 xml 文件中搜索数据集 [英] search dataset from xml file

查看:28
本文介绍了从 xml 文件中搜索数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要过滤加载 xml 文件时获得的结果.例如,我需要在 xml 数据中搜索带有关键字Chemistry"的项目.下面的 xml 示例是我的 xml 文件的摘要.数据加载到网格视图中.你能帮忙吗?谢谢!

I need to filter the results I obtain when I load my xml file. For example I need to search the xml data for items with keyword "Chemistry" for example. The below xml example is a summary of my xml file. The data is loaded in a gridview. Could you help? Thanks!

Xml 文件(摘要):

Xml File (summary):

<合同><合同><CONTRACTID>779</CONTRACTID><NAME>合同名称</NAME><KEYWORDS>化学、工程、化学</KEYWORDS><CONTRACTSTARTDATE>1/8/2005</CONTRACTSTARTDATE><CONTRACTENDDATE>31/7/2008</CONTRACTENDDATE><COMMODITIES><COMMODITY><COMMODITYCODE>CHEM</COMMODITYCODE><COMMODITYNAME>化学品</COMMODITYNAME></COMMODITY></COMMODITIES></CONTRACT></CONTRACTS>

我的代码背后的代码是:

My code behind code is:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim ds As DataSet = New DataSet()

    ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "/testxml.xml")

    Dim dtContract As DataTable = ds.Tables(0)
    Dim dtJoinCommodities As DataTable = ds.Tables(1)
    Dim dtCommodity As DataTable = ds.Tables(2)
    dtContract.Columns.Add("COMMODITYCODE")
    dtContract.Columns.Add("COMMODITYNAME")

    Dim count As Integer = 0
    Dim commodityCode As String = Nothing
    Dim commodityName As String = Nothing

    Dim dRowJoinCommodity As DataRow
    Dim trimChar As Char() = {","c, " "c}

    Dim textboxstring As String = "KEYWORDS like 'pencil'"

        For Each dRow As DataRow In dtContract.Select(textboxstring)

        commodityCode = ""
        commodityName = ""

        count = dtContract.Rows.IndexOf(dRow)
        dRowJoinCommodity = dtJoinCommodities.Rows(count)

        For Each dRowCommodities As DataRow In dtCommodity.Rows
            If dRowCommodities("COMMODITIES_Id").ToString() = dRowJoinCommodity("COMMODITIES_ID").ToString() Then

                commodityCode = commodityCode + dRowCommodities("COMMODITYCODE").ToString() + ", "

                commodityName = commodityName + dRowCommodities("COMMODITYNAME").ToString() + ", "

            End If

        Next

       commodityCode = commodityCode.TrimEnd(trimChar)
        commodityName = commodityName.TrimEnd(trimChar)
        dRow("COMMODITYCODE") = commodityCode
        dRow("COMMODITYNAME") = commodityName

    Next


    GridView1.DataSource = dtContract
    GridView1.DataBind()
End Sub

推荐答案

我必须建议您查看 XQuery 以及如何在 VB 中使用它,因为它正是为此类场景而设计的.一开始它像大多数语言一样有点吓人,但它非常强大,可以让你做的事情变得微不足道.

I must suggest that you check out XQuery and how to use it in VB since it's designed for precisely this sort of scenarios. It's a bit intimidating at first like most languages, but it's quite powerful and will make what you're doing trivial.

XQuery 是一种用于描述 XML 搜索的语言.XPath 是一个搜索字符串,用于描述 XML 中内容的路径".您可以通过元素名称、元素值、属性名称/值等轻松查找内容.

XQuery is a language for describing searches in XML. XPath is a search string describing the "path" of stuff in XML. You can easily look up things by element name, element value, attribute name/value etc.

很遗憾,我不懂 Visual Basic,从我大约 16 岁起就没有使用过它,所以我无法为您提供更多帮助,抱歉.但是您永远不会后悔学习如何使用 XQuery.它真正展现了 XML 的真正威力.

Unfortunately I don't know Visual Basic and haven't used it since I was about 16 so I can't help you more than that, sorry. But you will never regret learning how to use XQuery. It really brings out the true power of XML.

对于它的价值(在您弄清楚如何在 VB 中使用 XQuery 之前,肯定不是很多),以下是一些配置 XPath 搜索的示例:

For what it's worth (admittedly not a lot until you figure out how to use XQuery in VB), here are some examples of what you can do to configure XPath searches:

http://wxww.w3schools.com/xpath/xpath_functions.asp

同样,不要被它吓倒,只要找到一些用于 VB 的基本 XQuery 教程,您很快就会明白的.:)

Again, don't be intimidated by it, just find some basic XQuery tutorials for VB and you'll figure it out in no time. :)

这篇关于从 xml 文件中搜索数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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