insert Image into my XML
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: insert Image into my XML

  1. #1
    Join Date
    Dec 2005
    Posts
    35

    insert Image into my XML

    i have my file call quiz.xml.
    which i apply it into my asp.net

    my code are like this

    -----------------------------------------------------------------------

    <quiz xsi:noNamespaceSchemaLocation="quiz.xsd">
    <mchoice>
    <question>When you create a new document, it always displays behind the Toolbox. Which of the following actions will ensure it always opens flush with the Toolbox?</question>
    <answer>Using the keyboard shortcut key Ctrl+N [Cmd+N] to always create a new document</answer>
    <answer>Making sure the Options bar is not parked flush with the top of the screen; allowing a gap of a few pixels in between</answer>
    <answer>Holding down the Alt [Opt] key as you click the OK button in the New dialog</answer>
    <answer correct="yes">Moving the Toolbox a few pixels away from the edge of the screen</answer>
    </mchoice>
    </quiz>

    -----------------------------------------------------------------------

    How if I will like to add image into my tag or tag. Or is there any other way I can add and images.

  2. #2
    Join Date
    Aug 2005
    Posts
    132

    Re: insert Image into my XML

    There are lots of ways you can add images. YOu can add an image reference tag if the image is already hosted somewhere. You can serialise the image and then de-serialise it on the client end.

    Actually, there are two ways you can add images. (that I know how to do).

  3. #3
    Join Date
    Dec 2005
    Posts
    35

    Cool Re: insert Image into my XML

    Quote Originally Posted by GuOddian
    There are lots of ways you can add images. YOu can add an image reference tag if the image is already hosted somewhere. You can serialise the image and then de-serialise it on the client end.

    Actually, there are two ways you can add images. (that I know how to do).
    Can you show me the example? I trying to add a [img] inside but it do not work.

  4. #4
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315

    Re: insert Image into my XML

    It's quite impossible to give you a definete answer with what little information you have provided.

    You say you apply it into asp.net ?

    So presumably you are using a webbrowser to view the quiz ?

    Webbrowsers mostoften relies on something called html to render text and images, with an image referenced in the html code like this <img src="url"/>

    So probably you have some asp.net code that translates your XML into HTML.

    But without any info on how this translation is done, it's quite impossible to help you.
    The biggest problem encountered while trying to design a system that was completely foolproof,
    was, that people tended to underestimate the ingenuity of complete fools.
    Douglas Adams

  5. #5
    Join Date
    Dec 2005
    Posts
    35

    Re: insert Image into my XML

    my total code is like this
    and now I am confusing
    -----------------------------------------------------------------------
    <%@ Import Namespace="System.Xml" %>

    <script language="VB" runat="server">

    'Relative file path to XML data
    Dim strXmlFilePath as String = Server.MapPath("quiz.xml")

    Dim xDoc as XmlDocument = New XmlDocument()
    Dim intTotalQuestion as Integer
    Dim intQuestionNo as Integer = 1
    Dim intScore as Integer = 0
    Dim arrAnswerHistory As New ArrayList()
    Dim setPost As String

    Sub Page_Load(src as Object, e as EventArgs)

    'Load xml data
    xDoc.Load(strXmlFilePath)

    'Start a new quiz?
    If Not Page.IsPostBack Then

    'Count total question
    intTotalQuestion = xDoc.SelectNodes("/quiz/mchoice").Count

    'Record start time
    ViewState("StartTime") = DateTime.Now

    ShowQuestion(intQuestionNo)
    End If
    End Sub


    Sub btnSubmit_Click(src as Object, e as EventArgs)

    'Retrieve essential variables from state bag
    intTotalQuestion = ViewState("TotalQuestion")
    intQuestionNo = ViewState("QuestionNo")
    intScore = ViewState("Score")
    arrAnswerHistory = ViewState("AnswerHistory")
    setPost = ViewState("Post")

    'Correct answer?
    If rblAnswer.SelectedItem.Value = ViewState("CorrectAnswer") Then
    intScore += 1
    arrAnswerHistory.Add(0)
    Else
    arrAnswerHistory.Add(rblAnswer.SelectedItem.Value)
    End If

    If intScore > 8 Then
    setPost = "Expertise"

    ElseIf intScore > 5 Then
    setPost = "Moderate"

    Else
    setPost = "Beginner"
    End If

    'End of quiz?
    If intQuestionNo = intTotalQuestion Then

    'Yes! Show the result...
    QuizScreen.Visible = False
    ResultScreen.Visible = True

    'Render result screen
    ShowResult()

    Else

    'Show another question...
    QuizScreen.Visible = True
    ResultScreen.Visible = False
    intQuestionNo += 1

    'Render next question
    ShowQuestion(intQuestionNo)
    End If
    End Sub


    Sub ShowQuestion(intQuestionNo as Integer)
    Dim xNodeList as XmlNodeList
    Dim xNodeAttr as Object
    Dim strXPath as String
    Dim i as Integer
    Dim tsTimeSpent as TimeSpan

    strXPath = "/quiz/mchoice[" & intQuestionNo.ToString() & "]"

    'Extract question
    lblQuestion.Text = intQuestionNo.ToString() & ". " & xDoc.SelectSingleNode(strXPath & "/question").InnerXml

    'Extract answers
    xNodeList = xDoc.SelectNodes(strXPath & "/answer")

    'Clear previous listitems
    rblAnswer.Items.Clear

    For i = 0 to xNodeList.Count-1

    'Add item to radiobuttonlist
    rblAnswer.Items.Add(new ListItem(xNodeList.Item(i).InnerText, i+1))

    'Extract correct answer
    xNodeAttr = xNodeList.Item(i).Attributes.ItemOf("correct")
    If not xNodeAttr is Nothing Then
    If xNodeAttr.Value = "yes" Then
    ViewState("CorrectAnswer") = i+1
    End If
    End If
    Next

    'Output Total Question
    lblTotalQuestion.Text = intTotalQuestion

    'Output Time Spent
    tsTimeSpent = DateTime.Now.Subtract(ViewState("StartTime"))
    lblTimeSpent.Text = tsTimeSpent.Minutes.ToString() & ":" & tsTimeSpent.Seconds.ToString()

    'Store essential data to viewstate
    ViewState("TotalQuestion") = intTotalQuestion
    ViewState("Score") = intScore
    ViewState("QuestionNo") = intQuestionNo
    ViewState("AnswerHistory") = arrAnswerHistory
    ViewState("Post") = setPost
    End Sub

    Sub ShowResult()
    Dim strResult as String
    'Dim intCompetency as Integer
    Dim i as Integer
    Dim strXPath as String
    Dim tsTimeSpent as TimeSpan

    tsTimeSpent = DateTime.Now.Subtract(ViewState("StartTime"))

    strResult = "<center>"
    strResult += "<h3>Quiz Result</h3>"
    strResult += "<p>Points: " & intScore.ToString() & " of " & intTotalQuestion.ToString()
    strResult += "<p>Your Competency: " & Int(intScore/intTotalQuestion*100).ToString() & "%"
    strResult += "<p>Time Spent: " & tsTimeSpent.Minutes.ToString() & ":" & tsTimeSpent.Seconds.ToString()
    strResult += "<p>Your level: " & setPost.tostring()
    strResult += "</center>"

    strResult += "<h3>Quiz Breakdown:</h3>"
    For i = 1 to intTotalQuestion
    strXPath = "/quiz/mchoice[" & i.ToString() & "]"
    strResult += "<b>" & i.ToString() & ". " & xDoc.SelectNodes(strXPath & "/question").Item(0).InnerXml & "</b><br>"
    If arrAnswerHistory.Item(i-1)=0 Then
    strResult += "<font color=""green""><b>Correct</b></font><br><br>"
    Else
    strResult += "<b>You answered:</b> " & xDoc.SelectNodes(strXPath & "/answer[" & arrAnswerHistory.Item(i-1).ToString() & "]").Item(0).InnerXml & "<br>"
    strResult += "<font color=""red""><b>Incorrect</b></font><br><br>"
    End If
    Next

    lblResult.Text = strResult
    End Sub

    </script>
    <html>
    <head>
    <title>[FYP] Adobe Photoshop Quiz</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <span id="QuizScreen" runat="server">
    <form id="form1" runat="server">
    <table width="100%" border="0" cellpadding="2" cellspacing="0">
    <tr class="heading">
    <td width="50%"><font color="white"><b>FYP Adobe Photoshop Quiz</b></font></td>
    <td width="50%" align="right"><font color="white"><b></b></font></td>
    </tr>
    <tr>
    <td colspan="2">
    <b><asp:label id="lblQuestion" runat="server" /></b><br>
    <asp:radiobuttonlist
    id="rblAnswer"
    RepeatDirection="vertical"
    TextAlign="right"
    RepeatLayout="table"
    runat="server" /><br>
    <asp:requiredfieldvalidator
    ControlToValidate="rblAnswer"
    ErrorMessage="Please pick an answer!"
    runat="server" /><br>
    <asp:button id="btnSubmit" cssclass="button" text=" Next " onClick="btnSubmit_Click" runat="server" />&nbsp;
    </td>
    </tr>
    <tr class="heading">
    <td width="50%"><font color="white"><b>Total <asp:label id="lblTotalQuestion" runat="server" /> questions</b></font></td>
    <td width="50%" align="right"><font color="white"><b>Time spent <asp:label id="lblTimeSpent" runat="server" /></b></font></td>
    </tr>
    </table>
    </form>
    </span>
    <span id="ResultScreen" runat="server">
    <asp:label id="lblResult" runat="server" />
    </span>
    </body>
    </html>

  6. #6
    Join Date
    Dec 2005
    Posts
    35

    Re: insert Image into my XML

    Quote Originally Posted by khp
    It's quite impossible to give you a definete answer with what little information you have provided.

    You say you apply it into asp.net ?

    So presumably you are using a webbrowser to view the quiz ?

    Webbrowsers mostoften relies on something called html to render text and images, with an image referenced in the html code like this <img src="url"/>

    So probably you have some asp.net code that translates your XML into HTML.

    But without any info on how this translation is done, it's quite impossible to help you.
    what will you do then?
    What I want is instead of giving a plain text as answer, maybe i will like to put an image as answer.

    Is there any better way to do?

  7. #7
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315

    Re: insert Image into my XML

    Well I'am not an ASP kind of guy, So I can't really help you much execpt to note that the your ASP code does not support embedding images into the XML.

    I'am guessing this line
    Code:
    rblAnswer.Items.Add(new ListItem(xNodeList.Item(i).InnerText, i+1))
    populates the answer options list, InnerText means just that, text. To create an image, you would either have to copy an img node from the XML (Not really sure this would work) or create an img html element in ASP, I have no clue how to do either in ASP.
    Last edited by khp; March 13th, 2006 at 01:24 PM.
    The biggest problem encountered while trying to design a system that was completely foolproof,
    was, that people tended to underestimate the ingenuity of complete fools.
    Douglas Adams

  8. #8
    Join Date
    Dec 2005
    Posts
    35

    Re: insert Image into my XML

    I found my solution. Just add this line of code when you want to insert an image or anything
    <![ CDATA [ what you want to insert put here]]>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center