469,265 Members | 2,020 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,265 developers. It's quick & easy.

how to read XML by using GetElementsByTagName

need help here.

here is my XML file.

<kelvin>
<module assignment="1" questionno.="0">
<question>asd</question>
<choice1>1</choice1>
<choice2>2</choice2>
<choice3>3</choice3>
<choice4>4</choice4>
<correctchoice>Choice 2</correctchoice>
</module>
<module assignment="1" questionno.="1">
<question>asdasd</question>
<choice1>11</choice1>
<choice2>22</choice2>
<choice3>33</choice3>
<choice4>44</choice4>
<correctchoice>Choice 2</correctchoice>
</module>
<module assignment="1" questionno.="2">
<question>dsfsdf</question>
<choice1>111</choice1>
<choice2>222</choice2>
<choice3>333</choice3>
<choice4>444</choice4>
<correctchoice>Choice 3</correctchoice>
</module>
</kelvin>


display question at question label.
display Choice1 at choice1 label.
display Choice2 at choice2 label.

i've tried, but my label display everyting in that particular element.

please help me.
Thank You.
Sep 23 '08 #1
11 3281
Dormilich
8,651 Expert Mod 8TB
do you use DOM or XSLT to do the transformation?

regards
Sep 23 '08 #2
XSLT for the transformation.
Sep 23 '08 #3
Dormilich
8,651 Expert Mod 8TB
what shall the output file look like?
Sep 23 '08 #4
just display out on the Label, Questions, Choices.
just display Selective Question only and not all Question under the element <question>. Or Choices <choice1>.

something like that.
Sep 23 '08 #5
Dormilich
8,651 Expert Mod 8TB
the text of <question> you can get with //question/text(). same goes for <choiceX> (though I'd rename it to <choice no="X">, because you can then loop over <choice>). a special <question> you get with //question/text()[ancestor::module/@questionno. = 'X']

regards
Sep 23 '08 #6
jkmyoung
2,057 Expert 2GB
Could we see the code you're using to get choice1?
Sep 23 '08 #7
XmlNodeList Choice1 = doc.GetElementsByTagName("choice1");
foreach (XmlNode Choice in Choice1)
{
AnsLabel1.Text += Choice.ChildNodes[0].Value;
}

the code for choice and Question is similar.

please help.
Sep 24 '08 #8
Dormilich
8,651 Expert Mod 8TB
actually, this is DOM (Javascript maybe). XSLT is totally different. for XSLT I've given you (one possibility of) the XPath expressions you'll need for XSL. although I have no idea yet, how you will build it into HTML (a script?).

regards
Sep 24 '08 #9
to: Dormilich ,
mind show me some example how to do the xslt code.

cause im really lost here.
thanks alot.
Sep 25 '08 #10
this is my.
MCQ.xslt.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="MCQ.xsl"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>Student Assignment</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>


<xsl:template match="question">
<b> Question: </b><xsl:value-of select="."/>
<xsl:value-of select="."/>
<br />
</xsl:template>

<xsl:template match="choice1">
<b> Choice 1: </b><xsl:value-of select="."/>
<br />
</xsl:template>

<xsl:template match="choice2">
<b> Choice 2: </b><xsl:value-of select="."/>
<br />
</xsl:template>

<xsl:template match="choice3">
<b> Choice 3: </b><xsl:value-of select="."/>
<br />
</xsl:template>

<xsl:template match="choice4">
<b> Choice 4: </b><xsl:value-of select="."/>
<br />
</xsl:template>

<xsl:template match="correctchoice">
<b> Correct Choice: </b><xsl:value-of select="."/>
<br />
</xsl:template>

</xsl:stylesheet>

i hope You guys can help.
=)
Sep 25 '08 #11
Dormilich
8,651 Expert Mod 8TB
er, it looks fine. this is, what your code displays:

Student Assignment

Question: asd
Choice 1: 1
Choice 2: 2
Choice 3: 3
Choice 4: 4
Correct Choice: Choice 2
Question: asdasd
Choice 1: 11
Choice 2: 22
Choice 3: 33
Choice 4: 44
Correct Choice: Choice 2
Question: dsfsdf
Choice 1: 111
Choice 2: 222
Choice 3: 333
Choice 4: 444
Correct Choice: Choice 3

unless I totally misunderstood your problem

regards
Sep 29 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Andrew Poulos | last post: by
2 posts views Thread by Scamjunk | last post: by
4 posts views Thread by Michael Munch | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.