Results 1 to 5 of 5

Thread: [RESOLVED] Need some XML help.

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Resolved [RESOLVED] Need some XML help.

    Basically, I have an XML file that will have something like this in it:

    Code:
    <question="What happened in 1492?">
    	<answer=true>Columbus sailed the Ocean blue!</answer>
    	<answer=false>I divided by Zero!</answer>
    	<answer=false>Muffins</answer>
    	<answer=false>Programming</answer>
    </question>
    Similar questions (with better answers) will also be there. I will have 5 files, each file having 10 question groups each. I have no clue how to read this properly, or how to tell what the answers are! I would also need help writing these too.

    XML was never my strongpoint, so any pointers/tips/tricks/code examples would be lovely!

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Need some XML help.

    Are you looking for something simple like this?
    http://msdn.microsoft.com/en-us/libr...ldocument.aspx
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Need some XML help.

    try this. it's hardcoded with a modified version (i had to fix the xml) of your xml, but can be easily changed to open + use an external xml file:
    Attached Files Attached Files

  4. #4

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Need some XML help.

    Hey .paul., thanks for that...I hate to bother you with this though. I did fail to mention I was restricted to 2.0, since I don't know if the school computers are updated enough. If there is a possibility of using XmlTextReader possibly instead of LINQ, I'll hop on that.

    @sapator, I'll look into it.

  5. #5

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Need some XML help.

    I fiddled around with XmlDocument, and..now I have this:
    Code:
    System.Xml.XmlException was unhandled
      LineNumber=1
      LinePosition=1
      Message=Data at the root level is invalid. Line 1, position 1.
      Source=System.Xml
      SourceUri=""
      StackTrace:
           at System.Xml.XmlTextReaderImpl.Throw(Exception e)
           at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
           at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
           at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
           at System.Xml.XmlTextReaderImpl.Read()
           at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
           at System.Xml.XmlDocument.Load(XmlReader reader)
           at System.Xml.XmlDocument.LoadXml(String xml)
           at WorldHistoryGame.Questions..ctor(String questions) in E:\Documents and Settings\formlesstree4\my documents\visual studio 2010\Projects\WorldHistoryGame\WorldHistoryGame\classes\Questions.vb:line 74
           at WorldHistoryGame.LoginForm.OK_Click(Object sender, EventArgs e) in E:\Documents and Settings\formlesstree4\my documents\visual studio 2010\Projects\WorldHistoryGame\WorldHistoryGame\LoginForm.vb:line 5
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at WorldHistoryGame.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    Here's the code I'm using:

    Code:
    Public Sub New(ByVal questions As String)
            Dim reader As New XmlDocument With {.PreserveWhitespace = True}
            reader.LoadXml(questions) '<- errors right here.
    
            Dim QuestionXmlList As XmlNodeList = reader.SelectNodes("question")
            If QuestionXmlList.Count > 0 Then
    
                For Each item As XmlNode In QuestionXmlList
                    'Setup a loop for each <question> area.
                    Dim NQ As New QuestionData
                    With item
                        NQ.Question = .Item("ask").InnerText
                        NQ.Answer(1) = .Item("answer1").InnerText
                        NQ.Answer(2) = .Item("answer2").InnerText
                        NQ.Answer(3) = .Item("answer3").InnerText
                        NQ.Answer(4) = .Item("answer4").InnerText
                        NQ.CorrectAnswer = CInt(.Item("correct").InnerText)
                    End With
                    QuestionList.Add(NQ)
                Next
            End If
        End Sub
    And this is the XML I'm using....
    Code:
    <question>
    	<ask>What important event happened in 1492?</ask>
    	<answer1>Columbus sailed the Ocean blue!</answer1>
    	<answer2>I divided by Zero!</answer2>
    	<answer3>Muffins</answer3>
    	<answer4>Programming</answer4>
    	<correct>1</correct>
    </question>

    EDIT: Scratch that entire mess. I changed it to just Load() instead of LoadXml....no more errors and it reads it perfectly. I'm gonna mark this as resolved.

Posting Permissions

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



Click Here to Expand Forum to Full Width