Results 1 to 3 of 3

Thread: [RESOLVED] How to identify an xml empty tag using the XmlNodeType object?

  1. #1

    Thread Starter
    Hyperactive Member Working.Net's Avatar
    Join Date
    Aug 2010
    Posts
    389

    Resolved [RESOLVED] How to identify an xml empty tag using the XmlNodeType object?

    Hi,

    I have a program that analyzes and processes a complex xml data source (without a schema). The initial part of the program reads the xml line by line and captures the data value, table name and field name for further processing. The data (xml file) is created using InfoPath. This worked fine until we started using InfoPath 2010, which uses the "empty tag" (<tag />) for empty values instead of the traditional tags with no value (<tag></tag>). Of course my program does not know what to do with the empty tags and just ignores them - no exception, and no error.

    My question is: Does anyone know how to identify the empty tag using the XmlNodeType object attributes (in blue)?

    Thanks for your help and the code is below.

    Code:
        Private Sub Process_ReportData(ByVal vFilePath As String, ByVal vFileName As String)
            ' Create an instance of XmlTextReader and call Read method to read the(file)
            Dim RecordsTableAdapter As New RecordsTableAdapter
            Dim ReportsTableAdapter As New ReportsTableAdapter
    
            Dim textReader As XmlTextReader
            Dim vTableName As String = ""
            Dim vFieldName As String = ""
            Dim vElementMode As String = ""
            Dim vValue As String = ""
            Dim vDivMode As Boolean = False
            Dim vFieldMode As Boolean = False
            Dim vElementType As String = ""
            Dim vElementName As String = ""
            Dim vProjectNo As String = ""
    
            Try
                textReader = New XmlTextReader(vFilePath & vFileName)
                ' Check if an existing record exists. CheckIfRecordExists() willprovide user feedback
                If CheckIfRecordExists(vFileName) Then
                    ' Record already exists.  Skip this file and notify the user.
                    NewLineToTextBox("ALERT: Data from " & vFileName & " has already been uploaded and cannot be processed again!", True)
                Else
                    ' No existing records were found, continue upload process 
                    ' Delete all records from Records
                    RecordsTableAdapter.DELETE_Records_All()
                    ' Read current XML file name into Records table
                    RecordsTableAdapter.INSERT_Records("Reports", "XMLfileName", vFileName)
    
                    ' Process records in Records table
                    While textReader.Read()
                        vElementName = textReader.Name
                        'NewLineToTextBox(XmlNodeType.Element.ToString & ">" & vElementName & ">" ProcessElementName(vElementName, "Action"))
                        Select Case textReader.NodeType
                            Case XmlNodeType.Element
                                'Case "starting" Element
                                vElementMode = ProcessElementName(vElementName, "Action")
                                Select Case vElementMode
                                    Case "Field"
                                        vFieldMode = True
                                        vTableName = ProcessElementName(vElementName, "Table")
                                        vFieldName = ProcessElementName(vElementName, "Field")
                                    Case "divMode"
                                        vDivMode = True
                                End Select
                            Case XmlNodeType.Text
                                If vFieldMode And textReader.HasValue Then
                                    If vDivMode Then
                                        vValue = vValue & " ~ " & textReader.Value
                                    Else
                                        ' If the field name is a picture field
                                        vValue = textReader.Value
                                    End If
                                End If
                            Case XmlNodeType.EndElement
                                ' Process the data and reset the variables
                                If ProcessElementName(vElementName, "Field") = vFieldName Then
                                    RecordsTableAdapter.INSERT_Records(vTableName, vFieldName, vValue)
                                    'TransferAllData(vTableName, vFieldName, vValue)
                                    vFieldName = ""
                                    vValue = ""
                                    vFieldMode = False
                                    vDivMode = False
                                End If
                        End Select
                    End While
                    RecordsTableAdapter.Dispose()
    
                End If
            Catch Ex As XmlException
                NewLineToTextBox("ERROR 001: " & Ex.ToString, True)
            End Try
        End Sub

    As I stand here, on the fringes of my understanding, and look out over the
    vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: How to identify an xml empty tag using the XmlNodeType object?

    XmlNodeType should be Element then you'd need to check if XmlTextReader.IsEmptyElement = True.

    Here's the msdn documentation for the IsEmptyElement property: XmlTextReader.IsEmptyElement Property
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3

    Thread Starter
    Hyperactive Member Working.Net's Avatar
    Join Date
    Aug 2010
    Posts
    389

    Re: How to identify an xml empty tag using the XmlNodeType object?

    Thanks Matt.
    It took a while to figure out how to integrate your suggestion, and as things stand now I had to duplicate some code to get it to work, something I am not too happy about, but life and work must go on.

    Basically in my code, as shown above, if an empty element occurred I was bypassing the assignment of the field name and with an empty element the Case XmlNodeType.EndElement condition, where each of the records was inserted, was never reached. So I modified the Case XmlNodeType.Element condition to assign a field name and insert the record when the Element is empty (being that it indicates the end of a tag):

    Code:
        Case XmlNodeType.Element
            'Case "starting" Element
            vElementMode = ProcessElementName(vElementName, "Action")
            If textReader.IsEmptyElement Then
                ' If textReader.IsEmptyElement then we still need the field name.  The table name
                ' is still populated from the previous pass and the value is already set to ""
                vFieldName = ProcessElementName(vElementName, "Field")
                If ProcessElementName(vElementName, "Field") = vFieldName Then
                    RecordsTableAdapter.INSERT_eFieldsRecords(vTableName, vFieldName, vValue)
                    vFieldName = ""
                    vValue = ""
                    vFieldMode = False
                    vDivMode = False
                End If
            Else
                Select Case vElementMode
                    Case "Field"
                        vFieldMode = True
                        vTableName = ProcessElementName(vElementName, "Table")
                        vFieldName = ProcessElementName(vElementName, "Field")
                    Case "divMode"
                        vDivMode = True
                End Select
            End If
    In hindsight it does seem rather shortsighted to create an empty condition that is not recognized by the 10 or so default XmlNodeTypes. Well that is .NET for you. Makes me feel like a grunt: "Mo Work?"
    Last edited by Working.Net; May 23rd, 2012 at 08:31 AM.

    As I stand here, on the fringes of my understanding, and look out over the
    vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .

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