Thread: Parsing XML

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    387
    Rep Power
    21

    Parsing XML


    hi all im parsing an xml doc using VB.net:

    Code:
      Dim elemList as XmlNodeList = xDoc.GetElementsByTagName("z:row")            
    For i As Integer = 0 To elemList.Count - 1 	
    title = elemList(i).Attributes("ows_Title").Value 
    intro = elemList(i).Attributes("ows_Intro").Value  	 	
    	
    'OUTPUT HERE 	
    content +="<a href="""">" + title + "</a> - " + intro + "<br />"  	 
    Next
    but the problem is that sometimes the 'intro' var is blank (because it is not a compulsory field). but this blank value causes a "reference not set to an instance of an object" so i have tried to use the following:

    Code:
    if elemList(i).Attributes("ows_Intro").Value <> nothing 	
    intro = elemList(i).Attributes("ows_Intro").Value 	
    end if
    but i still get the same error. any ideas?!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    122
    Rep Power
    9
    Nothing is not a integer.....
    change <> to isnot and that should work.....

    or you can do somting like this

    This is from one of my projects but u can see what im getting at "isNot"

    then just fill the attribute with "none" until it is needed. then a if stament to find out if the atribute is "none" or not

    Code:
     Dim file As System.IO.StreamWriter
    
                                My.Computer.FileSystem.DeleteFile(OpenFileLocation)
                                file = My.Computer.FileSystem.OpenTextFileWriter(OpenFileLocation, True)
                                Dim i = 1
                                file.WriteLine("<?xml version='1.0' encoding='utf-8'?>")
                                file.WriteLine("<nodesteps>")
                                For Each arrayvalue In actionarray1
                                    If i = actionarray1.Count Then
                                        Exit Sub
                                    End If
                                    If arrayvalue IsNot Nothing Then
                                        file.WriteLine("<step index='" & i & "' type='" & actionarray1(i) & "' findtype='" & actionvalue2(i) & "' findtype2='" & actionvalue1(i) & "' findtext='" & actionvalue3(i) & "' filltext='" & actionvalue4(i) & "' waitinterval='" & actionvalue7(i) & "' selectindex='" & actionvalue6(i) & "'>")
                                        file.WriteLine("<" & Replace(actionarray1(i), " ", "") & ">" & actionvalue5(i) & "</" & Replace(actionarray1(i), " ", "") & ">")
                                        file.WriteLine("</step>")
                                        i += 1
                                    End If
                                Next
                                file.WriteLine("</nodesteps>")
                                file.Close()

    Comments on this post

    • delboy31 agrees : brilliant thanks. you can probably tell im new to VB
    Last edited by IMaVBNoob; August 9th, 2012 at 07:02 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by IMaVBNoob
    Nothing is not a integer.....
    change <> to isnot and that should work.....

    or you can do somting like this

    This is from one of my projects but u can see what im getting at "isNot"

    then just fill the attribute with "none" until it is needed. then a if stament to find out if the atribute is "none" or not

    Code:
     Dim file As System.IO.StreamWriter
    
                                My.Computer.FileSystem.DeleteFile(OpenFileLocation)
                                file = My.Computer.FileSystem.OpenTextFileWriter(OpenFileLocation, True)
                                Dim i = 1
                                file.WriteLine("<?xml version='1.0' encoding='utf-8'?>")
                                file.WriteLine("<nodesteps>")
                                For Each arrayvalue In actionarray1
                                    If i = actionarray1.Count Then
                                        Exit Sub
                                    End If
                                    If arrayvalue IsNot Nothing Then
                                        file.WriteLine("<step index='" & i & "' type='" & actionarray1(i) & "' findtype='" & actionvalue2(i) & "' findtype2='" & actionvalue1(i) & "' findtext='" & actionvalue3(i) & "' filltext='" & actionvalue4(i) & "' waitinterval='" & actionvalue7(i) & "' selectindex='" & actionvalue6(i) & "'>")
                                        file.WriteLine("<" & Replace(actionarray1(i), " ", "") & ">" & actionvalue5(i) & "</" & Replace(actionarray1(i), " ", "") & ">")
                                        file.WriteLine("</step>")
                                        i += 1
                                    End If
                                Next
                                file.WriteLine("</nodesteps>")
                                file.Close()
    Actually, if you compare Nothing to an Integer type, it would evaluate as true, so long as the integer is the default for that value type, which is 0.

    Now, you could also use LINQ:
    Code:
    Dim XMLElement =
    	<elemrow ows_Title="value1Title" ows_Intro="value1Intro">Element Row</elemrow>
    
    Dim attrib = XMLElement.Attributes().FirstOrDefault(Function(A) A.Name = "test")
    If attrib IsNot Nothing Then Console.WriteLine(attrib.Value) _
    	Else MessageBox.Show("Value is Null")

IMN logo majestic logo threadwatch logo seochat tools logo