how to read XML by using GetElementsByTagName

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mc Johnson
    New Member
    • Sep 2008
    • 7

    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>asdas d</question>
    <choice1>11</choice1>
    <choice2>22</choice2>
    <choice3>33</choice3>
    <choice4>44</choice4>
    <correctchoice> Choice 2</correctchoice>
    </module>
    <module assignment="1" questionno.="2" >
    <question>dsfsd f</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.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    do you use DOM or XSLT to do the transformation?

    regards

    Comment

    • Mc Johnson
      New Member
      • Sep 2008
      • 7

      #3
      XSLT for the transformation.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what shall the output file look like?

        Comment

        • Mc Johnson
          New Member
          • Sep 2008
          • 7

          #5
          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.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            how to read XML by using GetElementsByTa gName

            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::modul e/@questionno. = 'X']

            regards

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Could we see the code you're using to get choice1?

              Comment

              • Mc Johnson
                New Member
                • Sep 2008
                • 7

                #8
                XmlNodeList Choice1 = doc.GetElements ByTagName("choi ce1");
                foreach (XmlNode Choice in Choice1)
                {
                AnsLabel1.Text += Choice.ChildNod es[0].Value;
                }

                the code for choice and Question is similar.

                please help.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  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

                  Comment

                  • Mc Johnson
                    New Member
                    • Sep 2008
                    • 7

                    #10
                    to: Dormilich ,
                    mind show me some example how to do the xslt code.

                    cause im really lost here.
                    thanks alot.

                    Comment

                    • Mc Johnson
                      New Member
                      • Sep 2008
                      • 7

                      #11
                      this is my.
                      MCQ.xslt.

                      <?xml version="1.0" encoding="UTF-8"?>
                      <?xml-stylesheet type="text/xsl" href="MCQ.xsl"? >
                      <xsl:styleshe et 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="correctc hoice">
                      <b> Correct Choice: </b><xsl:value-of select="."/>
                      <br />
                      </xsl:template>

                      </xsl:stylesheet>

                      i hope You guys can help.
                      =)

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        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

                        Comment

                        Working...