如何让xml选择??? [英] How to make a xml to select ???

查看:66
本文介绍了如何让xml选择???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下xml文件:

i am using the following xml file:



< medpricer>

< search>< ; / search>

< medicine>

< id> 19< / id>

< title> Accu-Chek Mobile< /标题>

将强度> 500毫克< /强度>

将最低票价:CHEAPEST_PRICE> 30.0< /最低票价:CHEAPEST_PRICE>

将/医药> ; $
< medicine>

< id> 21< / id>

< title> Dr Hauschka Rosencreme< / title> <登记/>
将强度> 500毫克< /强度>

将最低票价:CHEAPEST_PRICE> 26.0< /最低票价:CHEAPEST_PRICE>

将/医药>

< medicine>

< id> 77< / id>

< title> Naproxen Mylan< / title>

< strength> 50mg< / strength>

< cheapest_price> 39.5< / cheapest_price>

< / medicine>

< / medpricer>


<medpricer>
<search></search>
<medicine>
<id>19</id>
<title>Accu-Chek Mobile</title>
<strength>500mg</strength>
<cheapest_price>30.0</cheapest_price>
</medicine>
<medicine>
<id>21</id>
<title>Dr Hauschka Rosencreme</title>
<strength>500mg</strength>
<cheapest_price>26.0</cheapest_price>
</medicine>
<medicine>
<id>77</id>
<title>Naproxen Mylan</title>
<strength>50mg</strength>
<cheapest_price>39.5</cheapest_price>
</medicine>
</medpricer>

 

现在在此文件中我希望显示完整的详细信息用户在textBox2中输入名称的药物,并在按钮点击时显示。

now in this file i wish to display the complete details of the medicine whose name has been entered by the user in the textBox2 and display it on button click.

虽然下面显示的代码工作正常,但用户必须写下药物的名称正确的拼写和正确的情况。我希望这个查询更"智能"。因此它可以忽略来自
用户输入的区分大小写,也可以选择所需的药物或具有相似名称的药物,即使用户拼写错误名称也是如此。

although the code shown below works fine but the user has to write the name of the medicine with correct spelling and with the correct case as well. i wish to make this query more "intelligent" so that it can ignore the case sensitivity from the user's input and also can select the desired medicine or the medicines with similar names even if the name is mis spelled by the user.

 

我的代码如下:

 

使用系统;

使用System.Collections;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Text;

使用System.Windows.Forms;

使用System.Xml;

使用System .Xml.XPath;
$




名称空间WindowsApplication10

{



   公共部分类Form1:表格

    {
$




        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender,EventArgs e)

        {

        }


        private void button1_Click(object sender,EventArgs e)

        {

            XPathNavigator导航;

            XPathDocument docNav;



$
            String strExpression;

            //打开XML。



$


       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; docNav = new XPathDocument(@" C:\Users\Raavan \Desktop\med.xml");
$


      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; nav = docNav.CreateNavigator();

            strExpression =" / medpricer / medicine [title ='" + textBox2.Text +"'] / title" ;;
$




$
b $ b           创建XPathNodeIterator XNI =(创建XPathNodeIterator)nav.Evaluate(strExpression);



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;的foreach(对象objValue在((IEnumerable的)nav.Evaluate(strExpression)))

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {



                textBox1.Text + = objValue.ToString();
$


            }


        }




    }
}

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;


namespace WindowsApplication10
{

    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XPathNavigator nav;
            XPathDocument docNav;


            String strExpression;
            // Open the XML.



            docNav = new XPathDocument(@"C:\Users\Raavan\Desktop\med.xml");

            nav = docNav.CreateNavigator();
            strExpression = "/medpricer/medicine[title='" + textBox2.Text + "']/title";




            XPathNodeIterator xni = (XPathNodeIterator)nav.Evaluate(strExpression);

            foreach (object objValue in ((IEnumerable)nav.Evaluate(strExpression)))
            {

                textBox1.Text += objValue.ToString();

            }

        }


    }
}

推荐答案

您可以使用XPath函数:contains&翻译( http://msdn.microsoft.com/en-us/library/ms256180.aspx

/ medpricer /药[含有(平移(标题, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),翻译( '" + textBox2.Text + QUOT;', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))]

/medpricer/medicine[contains(translate(title, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), translate('" + textBox2.Text + "', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))]


这篇关于如何让xml选择???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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