使用XML显示一组问题 [英] To display a set of questions using XML

查看:57
本文介绍了使用XML显示一组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用XML显示一组问题和这些问题的4个答案.

我的XML文件中总共有15个问题.但是我只能显示前两个问题.我无法追踪.

下面是代码.

Hello All,

I am trying to display a set of questions and 4 answers to these questions using XML.

There are total of 15 questions in my XML File. But i am able to display only first 2 questions. I am unable to trace that.

Below is the code.

public partial class _Default : System.Web.UI.Page 
{
    int intTotalQuestion;
    int intQuestionNo=1;
    XmlDocument xdoc = new XmlDocument();

    protected void Page_Load(object sender, EventArgs e)
    {
        string XmlPath = Server.MapPath("Business-Questions.xml");
        intTotalQuestion = xdoc.SelectNodes("quiz/mchoice").Count;
        xdoc.Load(XmlPath);
        ShowQuestion(intQuestionNo);

    }
    

    public void ShowQuestion(int intQuestionNo)
    {
        XmlNodeList xNodeList;
        
        string strXpath;
        int i;
        strXpath = "/quiz/mchoice[" + intQuestionNo.ToString() + "]";
       
        Label1.Text = intQuestionNo.ToString() + "." + xdoc.DocumentElement.SelectSingleNode(strXpath + "/question").InnerXml;
         xNodeList = xdoc.SelectNodes(strXpath + "/answer");
        RadioButtonList1.Items.Clear();
        for (i = 0; i < xNodeList.Count;i++)
        {
            RadioButtonList1.Items.Add(new ListItem(xNodeList.Item(i).InnerText));
            
        }
        

        


    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (intQuestionNo != intTotalQuestion)
       
            intQuestionNo++;
            ShowQuestion(intQuestionNo);
        

        
    }
}





请注意,这与CP中给出的代码相同,但是我已将其转换为C#,这里是VB.NET.

因此,请不要提供链接,而且我很长一段时间都在尝试此方法,并在Google上进行了搜索....但我无法对其进行跟踪.

因此,请不要提供任何链接,因为在所有链接中仅重复出现同一示例,变量名称不同或数据不同.

因此,请对此进行测试并为我提供帮助....

问候
MK





Kindly note that this is the same code given in CP, but I have converted it into C# and here it was VB.NET.

Hence please do not give links, and also I am trying this from a long time and also googled it....But I am unable to trace it.

Hence please do not give any links, because in all the links only same example is re-occurring, either the variable names differ or the data differs.

Hence please test this and assist me....

Regards
MK

推荐答案

可以通过两件事来减轻此问题..

从您拥有适当的XML的想法开始.您可以使用 MSFT XML记事本2007 [ ^ ]

您可以继续使用Xpath,但是我不建议这样做..它不是长期解决方案的强项..我个人一直认为它是.. um ... Kludgey ..

我认为,如果您想走偏僻的路线,建议您完全忽略XPath,而只是将XML视为节点树(内置于XDoc)

******************首选解决方案*************************

使用数据协定和迭代器..

这可以处理类的填充,并且比XPath更具OOP的功能.
这就是我的方法,它可以正常工作..99%的时间,其余1%是因为我正在尝试做我不应该尝试做的事情; P
There are two things which can be done to alleviate this problem..

starting with the idea that you have proper XML. you can test this using MSFT XML Notepad 2007[^]

you can proceed using Xpath, however i don''t recommend this.. it isn''t as robust of a long term solution.. and I''ve always personally found it to be .. um ... Kludgey..

I think if you want to go a pathy route i''d recommend ignoring XPath altogether and just treating the XML as a node tree (built into XDoc)

****************** Preferred Solution *************************

Use Data Contracts and an iterator ..

this handles populating a class and is all around more OOP then XPath...
This is how I do it and it works flawlessly .. 99% of the time and that other 1% is because I''m trying to do something I shouldn''t be trying to do ;P


这篇关于使用XML显示一组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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