VB.Net XMLTextReader继续在儿童中跳过第一个元素

时间:2011-07-01 21:00:23

标签: xml vb.net

我希望有人能帮助我...我正在阅读从服务器创建的XML文件,一切都很好,除了当我尝试处理文件时,它总是跳过第一个子元素之一。当我在没有处理的情况下运行测试时,只需一个直读器并循环遍历元素,第一个元素就在那里。但是,只要我添加一个检查以查看该节点是否为Element,它就会跳过“Attachment”元素。这是我的XML:

<Reports xmlns="">
<FinalReport JobID="335417" LineItemID="400391" FileNumber="AGAINTEST" PropertyID="98765312" PONumber="" PropAddress="9255 John Street" PropCity="Fairfax" PropState="VA" PropZip="22132" OrderStatus="Delivered" ServiceName="Ext Property Inspection">
    <DataCapture>
        <!--Report saved: 7/1/2011 11:28:43 AM by: frank-->
        <report>
            <StreetVisible>No</StreetVisible>
            <Vacant>No</Vacant>
            <SaleSign>No</SaleSign>
            <RentSign>No</RentSign>
            <ExtCondition>Average</ExtCondition>
            <ComparedCondition>Similar</ComparedCondition>
            <DeferredMaintenance>No</DeferredMaintenance>
            <UnderConstruction>No</UnderConstruction>
            <SafetyIssues>No</SafetyIssues>
            <NaturalDisaster>No</NaturalDisaster>
            <PropertyLocation>Gated</PropertyLocation>
            <PropertyUse>Triplex</PropertyUse>
            <Nonresidential>No</Nonresidential>
            <Outbuildings>Shed</Outbuildings>
            <Garage>No</Garage>
            <Carport>No</Carport>
            <Capacity>2</Capacity>
            <AdverseFactors>Railroad</AdverseFactors>
            <PositiveFactors>GolfCourse</PositiveFactors>
            <PropertyType>CondoGarden</PropertyType>
            <HomeFront>Yes</HomeFront>
            <AddressPhoto>Yes</AddressPhoto>
            <StreetScene>Yes</StreetScene>
            <AnyComments>Test some comments here...</AnyComments>
            <FieldRepName>JOE INSPECTIONS</FieldRepName>
        </report>
    </DataCapture>
    <Attachment imageID="1988" extension="jpg" image_type="IMG" Keyed="0" ImageNote="" Image="Base64" />
    <Attachment imageID="1990" extension="jpg" image_type="IMG" Keyed="0" ImageNote="" Image="Base64" />
    <Attachment imageID="1991" extension="jpg" image_type="IMG" Keyed="0" ImageNote="" Image="Base64" />
    <Attachment imageID="1992" extension="jpg" image_type="IMG" Keyed="0" ImageNote="" Image="Base64" />
    <Attachment imageID="1993" extension="jpg" image_type="IMG" Keyed="0" ImageNote="" Image="Base64" />
    <Attachment imageID="1994" extension="jpg" image_type="IMG" Keyed="0" ImageNote="" Image="Base64" />
</FinalReport>
<PDFReport>Base64</PDFReport>

以下是我用来获取XML的代码 - 我删除了reader.Read()之间的所有内容,然后这个第一个“附件”元素似乎出现了。我甚至尝试删除If reader.IsStartElement()但仍然没有运气。

Using reader As XmlTextReader = New XmlTextReader(xdoc.OuterXml, XmlNodeType.Document, Nothing)

                        Dim xData As New OrderResults_Class

                        While reader.Read()

                            If reader.IsStartElement() Then
                                LogDebug.Append(vbCrLf & "Row: " & x & " Is Start: " & reader.IsStartElement() & " Node Type: " & reader.NodeType() & " Element: " & reader.Name & " >> Attributes: " & reader.HasAttributes.ToString & " Value: " & Left(reader.Value, 100))
                                ele = reader.Name

                                Select Case ele
                                    Case "Attachment"
                                        'LogDebug.Append(vbCrLf & "Row: (" & a & ") " & x & " Node Type: " & reader.NodeType() & " Element: " & reader.Name & " >> Attributes: " & reader.HasAttributes.ToString & " Value: " & Left(reader.Value, 100))
                                        If reader.HasAttributes Then
                                            Dim xDataImg As New NVMSOrderResults_Images_Class

                                            Dim myImage() As Byte = ConvertImageFromBase64(reader.GetAttribute("Image"))

                                            With xDataImg
                                                .JobID = CheckOrder
                                                .ImageRef = reader.GetAttribute("imageID")
                                                .FileNumber = rs("FileNumber").ToString
                                                .ImageDescription = reader.GetAttribute("ImageNote")
                                                .ImageType = reader.GetAttribute("extension")
                                                .ImageFile = myImage
                                            End With

                                            Dim ImgRet As String = Process_OrderResults_Images(xDataImg)
                                            If IsNumeric(ImgRet) Then
                                                LogMsg = "[" & rs("FileNumber").ToString & "] Photo Received: " & ImgRet
                                            Else
                                                LogMsg = "[" & reader.GetAttribute("imageID") & "] Error processing photo: " & ImgRet
                                            End If
                                            AppLogging.WriteTableLog(CheckOrder, LogMsg)
                                        End If

                                    Case "FinalReport"
                                        If reader.HasAttributes Then
                                            With xData
                                                .JobID = CheckOrder
                                                .LineItemID = reader.GetAttribute("LineItemID")
                                                .FileNumber = reader.GetAttribute("FileNumber")
                                                .PropertyID = reader.GetAttribute("PropertyID")
                                                .PropAddress = reader.GetAttribute("PropAddress")
                                                .PropCity = reader.GetAttribute("PropCity")
                                                .PropState = reader.GetAttribute("PropState")
                                                .PropZip = reader.GetAttribute("PropZip")
                                                .Service = reader.GetAttribute("ServiceName")
                                            End With
                                            'LogDebug.Append(vbCrLf & "Row: (" & i & ") " & x & " Node Type: " & reader.NodeType() & " Element: " & reader.Name & " >> Attributes: " & reader.HasAttributes.ToString & " Value: " & Left(reader.Value, 100))
                                        End If

                                    Case "DataCapture"
                                        xData.DataCollection = reader.ReadInnerXml

                                End Select
                            ElseIf reader.NodeType = XmlNodeType.Attribute Then
                                LogDebug.Append(vbCrLf & "This is an Attib: " & ele & " Is Start: " & reader.IsStartElement() & " Node Type: " & reader.NodeType() & " Element: " & reader.Name & " >> Attributes: " & reader.HasAttributes.ToString & " Value: " & Left(reader.Value, 100))
                            ElseIf reader.NodeType = XmlNodeType.Text Then
                                If ele = "PDFReport" Then
                                    xData.PDFReport = ConvertImageFromBase64(reader.Value)

                                    'Save Order Results ///////////////////////////
                                    Dim ResultRet As String = Process_OrderResults(xData)
                                    If IsNumeric(ResultRet) Then
                                        LogMsg = "[" & rs("FileNumber").ToString & "] Job Successfully Received: " & CheckOrder
                                    Else
                                        LogMsg = "[" & rs("FileNumber").ToString & "] Error processing results: " & ResultRet
                                    End If
                                    AppLogging.WriteTableLog(CheckOrder, LogMsg)
                                End If
                            End If

                            x += 1

                        End While

                        '// Write Debugging Text ////////////////////////////////////////////
                        AppLogging.WriteTableLog(CheckOrder, LogDebug.ToString)

                        y += 1
                    End Using

如果我错过了什么,请告诉我......我已经厌倦了看这段代码; D 谢谢!

1 个答案:

答案 0 :(得分:1)

XmlTextReader被记录为“仅向前”; 我个人对此的解释意味着如果你有属性x = 1 y = 2 z = 3 并尝试使用getattribute(“y”)然后跳过属性“x”并且无法返回到它 - 属性不是这样的哈希表,而是搜索到的字符串缓冲区

所以如果输入数据集中缺少一个属性(或者输入错误的顺序),这不意味着你会跳到下一个元素 - 我没有正确测试这个但是似乎匹配代码的行为我写过(然后重新写了)

只是我的2-penneth;我可能错了 - 我可能正朝着正确的方向前进,但现在还没有 - 只是想帮助