CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: XMLReader?

  1. #1
    Join Date
    Nov 2008
    Posts
    6

    Unhappy XMLReader?

    Hello Every1,

    I hope it is ok for me to post this thread here. I am new to VB .Net and am having difficulties knowing where to start with my new windows form application i am trying to build.

    What i want to create is a form where a user can enter XML into a textbox and then hit 'TEST' to validate the XML to see if there is any problems with it.

    I have built the form using VB express edition but now need to code and this is where i am coming across problems.

    This is how the programe should work:

    - A user selects a value from the combo box at the top of the form (This is the make of car. The xml structure changes for each value in the combobox)
    - The user then enters the XML into the textbox or can do file open and select the XML which will then be displayed in this text box
    - The user then hits 'Test' and the results of the XML are shown in another text box (Results Textbox) at the bottom of the form which highlight any problems with the XML or state if the XML is formatted correctly.

    Here is a example piece of the XML that will need to be processed:

    <File>
    <Car>
    <Vehicle Make="Ford">
    <CarDetails Model="Fiesta"
    Colour="Red"
    Numberofdoors="5" />
    <Owners Numberofowners="2"
    Year Registered="2006" />
    </Vehicle>
    <Car>
    </File>

    I want the XML to be read so that all the attributes values entered are compared to a preformed list to check if it is a valid value entered.

    For e.g If we look at the CarDetails node and the Model attribute, the value entered is Fiesta. I want my form to take this value and compare it against a list of valid values for this specific attribute, I.e Sierra, Escort, Fiesta. As Fiesta is on the valid entries list the program will continue reading the rest of the XML. If a value entered was 206 (A value that is not on accepted list) then the program continues to read the rest of the XML but highlights this problem in the results textbox. The user would then see what the problem is with the XML.

    If anybody can help with this it would be very much appreciated as i am getting very frustrated!

    Thank you!
    Chloe ~X~

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: XMLReader?

    Welcome to the Forums!

    This sounds suspiciously like a school project, but I'd recommend looking at the System.XML Namespace (and the XMLDocument class in it). In fact, the XML validation if extremely simple (as long as you don't mind only getting one error at a time...)
    Code:
    Dim A As New XmlDocument
    Try
      A.LoadXml(TextBox1.Text)
    Catch Exception As XmlException
      MsgBox(Exception.ToString)
    End Try
    As for other things, do some research into making schemas. The you should be able to define the correct values, and get the XML namespace to do all your work for you...
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    Nov 2008
    Posts
    6

    Re: XMLReader?

    Hi Javajawa!

    Thank you for this!

    I have a quick question, how do i get the message to appear in a text box rather than a pop up message box? I have a second text box on my form called TextBox2 which i want the errors to be displayed in?

    Also with this code it only looks for problems, so if the XML is formatted fine then there is no response, how can i add a message that says The XML is formatted correctly if there are problems with it?

    Thanks Hun,

    Chloe ~X~

  4. #4
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: XMLReader?

    You can set the text in a text box using TextBox2.Text =

    As for displaying an ok message, the best Idea is to have a variable which counts the errors found. Then at the end of the test, you see if this is equal to zero. If it is, you display the 'It's All Ok' message...
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  5. #5
    Join Date
    Nov 2008
    Posts
    6

    Re: XMLReader?

    Hi again,

    I have set the TextBox2 as below but it doesn't display the error:

    TextBox2.Text = (ex.Message)

    Have i done something wrong?

    How do i write a variable? Sorry for all the questions!

    Chloe ~X~

  6. #6
    Join Date
    Nov 2008
    Posts
    6

    Re: XMLReader?

    Hi,

    The TextBox2 message is working now!

    I just want to write a variable to say that if there are no errors then display a message in TextBox2 stating The XML is formatted correctly, how do i do this?

    Kind Regards,
    Chloe ~X~

  7. #7
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: XMLReader?

    Don't worry about the questions - Everyone is a beginner at some point.

    For the first part, I'm expect you're getting an error which isn't an XMLError, so it isn't processed in the catch section. The best way to check things is to set a breakpoint at the beginning of the procedure (F9) and step through and work out what is going on.

    As for working with variables, Here is the MSDN page
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  8. #8
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: XMLReader?

    I should pay more attention to replies, shouldn't I? :P

    I'd recommend defining an Integer called 'ErrorCount' inside the procedure (have a read through the link in my last post if you're unsure). Then, inside the catch statement, add 1 to ErrorCount - this will then count the errors (yes, there may only ever be one, but something might change...)

    Then, at the end of the procedure, see if it's 0, and change the text in the textbox accordingly.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  9. #9
    Join Date
    Nov 2008
    Posts
    6

    Re: XMLReader?

    Hi Javajawa!!

    This is my code and it worked!

    Dim xmlDoc As New Xml.XmlDocument
    Dim errorCount As Integer = 0
    Try
    xmlDoc.LoadXml(TextBox1.Text)
    Catch ex As Exception
    errorCount = 1
    TextBox2.Text = (ex.Message)
    End Try
    If errorCount = 0 Then
    TextBox2.Text = "The XML is formatted correctly"
    End If

    Thank you for your help!!

    What i need to do now is to get the program to check the values entered for each attribute in the XML is correct, so far i have this:

    Dim xmlDoc1 As New Xml.XmlDocument
    Dim xmlRoot As Xml.XmlNode
    xmlDoc.LoadXml(TextBox1.Text)
    xmlRoot = xmlDoc.DocumentElement
    Dim make As String = xmlRoot.SelectSingleNode("Car/Vehicle").Attributes("Make").Value
    Dim model As String = xmlRoot.SelectSingleNode("Car/Vehicle/CarDetails").Attributes("Model").Value
    Dim colour As String = xmlRoot.SelectSingleNode("Car/Vehicle/CarDetails").Attributes("Colour").Value
    Dim numberOfDoors As Integer = CInt(xmlRoot.SelectSingleNode("Car/Vehicle/CarDetails").Attributes("Numberofdoors").Value)
    Dim numberOfOwners As Integer = CInt(xmlRoot.SelectSingleNode("Car/Vehicle/Owners").Attributes("Numberofowners").Value)
    Dim yearRegistered As Integer = CInt(xmlRoot.SelectSingleNode("Car/Vehicle/Owners").Attributes("YearRegistered").Value)

    How and where do i write the valid values that can be used in the XML?

    For example the first atrribute value is 'Make' i want to be able to set 3 values that can be entered here, i.e AUDI FORD and VAUXHALL and so if BMW is entered this will return an error?

    Thank you again so much!

    Chloe~X~

  10. #10
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: XMLReader?

    May I ask where this is going? If this is some form of school work (and it does rather look like a school project), then all I can really do is to point you in the way of ENUMs, and Using the [ENUM].Parse method.
    Also, you might want to store the car details as a structure.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

Tags for this Thread

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

Featured