Click here to Skip to main content
15,889,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        

        
    }
}




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
Posted
Comments
Hiren solanki 14-Oct-10 1:39am    
Could you please show us your xml format ( single sample node ) ?
Mukund Kallapur 14-Oct-10 3:16am    
Hi Hiren,

Thanks for the response. Please find below the sample XML.

<quiz>


<mchoice>
<question>1.Which cricketer is the Brand Ambassador of Band Aid?

<answer correct="yes">i) Virender Sehwag</answer>
<answer>iI) Sachin Tendulkar</answer>
<answer>iii) Saurav Ganguly</answer>
<answer>iv) Yuvraj Singh</answer>


<mchoice>

<question>2.Paul Jacobs is the CEO of which of the below Companies?
<answer>i) Sun Microsystems</answer>
<answer>ii) EDS</answer>
<answer correct="yes">iii) Qualcomm</answer>
<answer>iv) Microsoft</answer>


<mchoice>
<question>3.Who coined the word "Transformational Leadership ?"
<answer>i) Pradeep Sindhu</answer>
<answer>ii) Philip Kotlar </answer>
<answer>iii) Samuel A Dipiazza</answer>
<answer correct="yes">iv) James McGregor Burns</answer>




<mchoice>
<question>3.Aditya Birla collaborated with which foreign company to roll out an
insurance scheme
<answer>i) Vysya</answer>
<answer correct="yes">ii) SunLife</answer>
<answer>iii) Sanmar</answer>
<answer>iv) Max New York Life</answer>


<mchoice>
<question>4.Citi Bank co brands for which Oil Company ?
<answer correct="yes">i) HP</answer>
<answer>ii) IOCL</answer>
<answer>iii) BPCL</answer>
<answer>iv) IBP</answer>


<mchoice>
Mukund Kallapur 14-Oct-10 3:18am    
<quiz>
<mchoice>
above questions are enclosed here
<mchoice>
Mukund Kallapur 14-Oct-10 3:18am    
<quiz>
<mchoice>
...
<mchoice>
Mukund Kallapur 14-Oct-10 3:19am    
quiz -- root element
in mchoice tags the questions are enclosed...

1 solution

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
 
Share this answer
 
Comments
Tarun.K.S 21-Oct-10 11:38am    
hi ely_bob, it would be really helpful if you could tell me why one should not use XPath. I am a big fan of Xpath by the way! :D
ely_bob 21-Oct-10 11:54am    
You can use it and it works fine, However I dislike the idea that you need to "treat" each tag, I find it easier to "treat" the entire object once and be done with it... That said if your working with an existing system that uses a lot of attributes, DataContracts is not the way to go and you should use XPath, but I am still a fence sitter in that particular .. um "discussion"...
As I see it..
Xpath Pros:
Very similar to XSLT in nature, with the allowance to look at anything in the document.
Handles Attributes.. WELL.
Xpath Cons:
You need to Parse the document value by value!

DataContract Pros:
You can parse the entire document to populate a class, quickly.
It is supported by WCF, so you can get experience in WCF by practicing DataContracts and their usage.
When the solution or the XML schema change the Data contract handles any changes which need to be made to the XML for you. (lazy initialization basically)
Data contract Cons:
DOES NOT handle attributes well (read at all)...
Can get tricky with namespace and Generics. (Read VERY Tricky)..

There is no go to reason to pick one over the other consistently in all cases, but personally i like the way Serialization is handled using DataContracts, so i try to get that to work and use XPath only when I have to. (hope that doesn't offend)

Tarun, what, if you would be so kind as to share, are your opinions/reasons on this?
Tarun.K.S 28-Oct-10 8:52am    
well actually i am still in the learning stage of "serialization" and i find it very interesting! as far as i have studied using serialization is efficient and is more of OOP as you said which is a good thing! So i can only say that till i complete studying DataContract Serialization, i love XPath :P hahahahaa!! but surely after that i will switch over to Serialization!! so now i can get experience in WCF too!

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