【问题标题】:VB 2010 XML - How to determine the descendant levelVB 2010 XML - 如何确定后代级别
【发布时间】:2017-08-01 07:36:45
【问题描述】:

出于必要,我对 VB 2010 的 System.XML 方面有了更多的了解。在解析由我们的现场人员使用的设备生成的某些 XML 文件时,我试图提取标签名称及其内部文本。这真的没问题,但我需要确定每个子节点的后代级别,以便以某种格式化的方式在文本框中显示结果。例如,来自下面的 XML 代码:

<document>
    <PointRecord ID="00000050" TimeStamp="2017-03-03T09:39:54">
        <Name>WF2510</Name>
        <Code>EOC RECT 6/</Code>
        <Method>StaticObservation</Method>
        <Classification>Normal</Classification>
        <Deleted>false</Deleted>

        <ECEFDeltas>
            <DeltaX>
              <Value>-14179.040909261</Value>
              <InTolerance>True</InTolerance>
            </DeltaX>
            <DeltaY>
              <Value>-3572.6636230592</Value>
              <InTolerance>True</InTolerance>
            </DeltaY>
            <DeltaZ>
              <Value>-8319.8607607852</Value>
              <InTolerance>False</InTolerance>
            </DeltaZ>
          </ECEFDeltas>
    </PointRecord>
</Document>

我想提取节点名称和内部文本,以便以更易读的格式呈现给非 XML 导向,如下所示:

Point Record ID: 00000050 Time Stamp: 2017-03-03 09:39:54
  Name: WF2510
  Code: EOC RECT 6
  Method: StaticObservation
  Classification: Normal
  Deleted: False
  ECFDeltas-
    DeltaX: -14179.040909261 In Tolerance: Yes
    DeltaY: -3572.6636230592 In Tolerance: Yes
    DeltaZ: -8319.8607607852 In Tolerance: No

我遇到的问题是知道如何将每个节点中包含的结果分组为子级别,以便缩进每个后代节点中子节点的名称和值,或者知道何时放置两个空格,四个空格等。我一直在尝试找到一个表示每个节点的后代级别的整数值,但我没有运气。

有人可以帮忙吗?

谢谢!

【问题讨论】:

  • xml 文档是可用节点的一个很好的例子吗?或者可能有更多或更少的节点和后代级别?
  • 文件中有数百个节点,但我只选择具有某个属性的那些节点。但是这些节点有几个级别的后代,因此后代级别因兄弟姐妹而异。我目前正在接近它,因此有嵌套的“for-each”循环可以深入到尽可能多的级别,但很难知道要钻多远并停止钻探。换句话说,要嵌套多少。谢谢你的关注。我希望我能正确解释这一点。
  • 我不确定您使用什么技术来读取 xml。我使用了很多 xml 文档,但它们总是遵循一些标准模型。我将根据您提供的模型发布答案。它可能适合您,也可能不适合您(但您可能会根据您的要求生成更多模型)。看我的回答。

标签: xml vb.net visual-studio-2010 xmlreader


【解决方案1】:

您有与您的 xml 架构匹配的 vb.net 类吗?如果没有,你可以按照这个帖子生成一个https://stackoverflow.com/a/17315863/832052

类应该是这样的:

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class Document

    Private pointRecordField() As DocumentPointRecord

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("PointRecord")> _
    Public Property PointRecord() As DocumentPointRecord()
        Get
            Return Me.pointRecordField
        End Get
        Set(value As DocumentPointRecord())
            Me.pointRecordField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecord

    Private nameField As String

    Private codeField As String

    Private methodField As String

    Private classificationField As String

    Private deletedField As Boolean

    Private eCEFDeltasField As DocumentPointRecordECEFDeltas

    Private idField As Byte

    Private timeStampField As Date

    '''<remarks/>
    Public Property Name() As String
        Get
            Return Me.nameField
        End Get
        Set(value As String)
            Me.nameField = value
        End Set
    End Property

    '''<remarks/>
    Public Property Code() As String
        Get
            Return Me.codeField
        End Get
        Set(value As String)
            Me.codeField = value
        End Set
    End Property

    '''<remarks/>
    Public Property Method() As String
        Get
            Return Me.methodField
        End Get
        Set(value As String)
            Me.methodField = value
        End Set
    End Property

    '''<remarks/>
    Public Property Classification() As String
        Get
            Return Me.classificationField
        End Get
        Set(value As String)
            Me.classificationField = value
        End Set
    End Property

    '''<remarks/>
    Public Property Deleted() As Boolean
        Get
            Return Me.deletedField
        End Get
        Set(value As Boolean)
            Me.deletedField = value
        End Set
    End Property

    '''<remarks/>
    Public Property ECEFDeltas() As DocumentPointRecordECEFDeltas
        Get
            Return Me.eCEFDeltasField
        End Get
        Set(value As DocumentPointRecordECEFDeltas)
            Me.eCEFDeltasField = value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()> _
    Public Property ID() As Byte
        Get
            Return Me.idField
        End Get
        Set(value As Byte)
            Me.idField = value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()> _
    Public Property TimeStamp() As Date
        Get
            Return Me.timeStampField
        End Get
        Set(value As Date)
            Me.timeStampField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltas

    Private deltaXField As DocumentPointRecordECEFDeltasDeltaX

    Private deltaYField As DocumentPointRecordECEFDeltasDeltaY

    Private deltaZField As DocumentPointRecordECEFDeltasDeltaZ

    '''<remarks/>
    Public Property DeltaX() As DocumentPointRecordECEFDeltasDeltaX
        Get
            Return Me.deltaXField
        End Get
        Set(value As DocumentPointRecordECEFDeltasDeltaX)
            Me.deltaXField = value
        End Set
    End Property

    '''<remarks/>
    Public Property DeltaY() As DocumentPointRecordECEFDeltasDeltaY
        Get
            Return Me.deltaYField
        End Get
        Set(value As DocumentPointRecordECEFDeltasDeltaY)
            Me.deltaYField = value
        End Set
    End Property

    '''<remarks/>
    Public Property DeltaZ() As DocumentPointRecordECEFDeltasDeltaZ
        Get
            Return Me.deltaZField
        End Get
        Set(value As DocumentPointRecordECEFDeltasDeltaZ)
            Me.deltaZField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltasDeltaX

    Private valueField As Decimal

    Private inToleranceField As String

    '''<remarks/>
    Public Property Value() As Decimal
        Get
            Return Me.valueField
        End Get
        Set(value As Decimal)
            Me.valueField = value
        End Set
    End Property

    '''<remarks/>
    Public Property InTolerance() As String
        Get
            Return Me.inToleranceField
        End Get
        Set(value As String)
            Me.inToleranceField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltasDeltaY

    Private valueField As Decimal

    Private inToleranceField As String

    '''<remarks/>
    Public Property Value() As Decimal
        Get
            Return Me.valueField
        End Get
        Set(value As Decimal)
            Me.valueField = value
        End Set
    End Property

    '''<remarks/>
    Public Property InTolerance() As String
        Get
            Return Me.inToleranceField
        End Get
        Set(value As String)
            Me.inToleranceField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class DocumentPointRecordECEFDeltasDeltaZ

    Private valueField As Decimal

    Private inToleranceField As String

    '''<remarks/>
    Public Property Value() As Decimal
        Get
            Return Me.valueField
        End Get
        Set(value As Decimal)
            Me.valueField = value
        End Set
    End Property

    '''<remarks/>
    Public Property InTolerance() As String
        Get
            Return Me.inToleranceField
        End Get
        Set(value As String)
            Me.inToleranceField = value
        End Set
    End Property
End Class

现在您在 vb.net 中有一个类,您可以将 xml 文档序列化和反序列化为该类的实例。

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Sub Main()

    Dim s = New XmlSerializer(GetType(Document))
    Dim d As Document
    ' use your filename here
    Using sr = New StreamReader("document.xml")
        d = CType(s.Deserialize(sr), Document)
    End Using
    For Each pr In d.PointRecord
        Console.WriteLine("Point Record ID: {0} Time Stamp: {1}",
                          pr.ID, pr.TimeStamp)
        Console.WriteLine("  Name: {0}",
                          pr.Name)
        Console.WriteLine("  Code: {0}",
                          pr.Code)
        Console.WriteLine("  Method: {0}",
                          pr.Method)
        Console.WriteLine("  Classication: {0}",
                          pr.Classification)
        Console.WriteLine("  Deleted: {0}",
                          pr.Deleted)
        Console.WriteLine("  ECFDeltas-")
        Console.WriteLine("    DeltaX: {0:0.000000000} In Tolerance: {1}",
                          pr.ECEFDeltas.DeltaX.Value, pr.ECEFDeltas.DeltaX.InTolerance)
        Console.WriteLine("    DeltaY: {0:0.000000000} In Tolerance: {1}",
                          pr.ECEFDeltas.DeltaY.Value, pr.ECEFDeltas.DeltaY.InTolerance)
        Console.WriteLine("    DeltaZ: {0:0.000000000} In Tolerance: {1}",
                          pr.ECEFDeltas.DeltaZ.Value, pr.ECEFDeltas.DeltaZ.InTolerance)
    Next
    Console.ReadLine()
End Sub

我添加了另一个&lt;PointRecord ID="00000050" TimeStamp="2017-03-03T09:39:54"&gt; 节点来生成具有PointRecord 数组的模型,假设你有这个。

<Document>
    <PointRecord ID="00000050" TimeStamp="2017-03-03T09:39:54">
        <Name>WF2510</Name>
        <Code>EOC RECT 6/</Code>
        <Method>StaticObservation</Method>
        <Classification>Normal</Classification>
        <Deleted>false</Deleted>

        <ECEFDeltas>
            <DeltaX>
              <Value>-14179.040909261</Value>
              <InTolerance>True</InTolerance>
            </DeltaX>
            <DeltaY>
              <Value>-3572.6636230592</Value>
              <InTolerance>True</InTolerance>
            </DeltaY>
            <DeltaZ>
              <Value>-8319.8607607852</Value>
              <InTolerance>False</InTolerance>
            </DeltaZ>
          </ECEFDeltas>
    </PointRecord>
    <PointRecord ID="00000051" TimeStamp="2017-03-03T09:39:54">
        <Name>WF2510</Name>
        <Code>EOC RECT 6/</Code>
        <Method>StaticObservation</Method>
        <Classification>Normal</Classification>
        <Deleted>false</Deleted>

        <ECEFDeltas>
            <DeltaX>
              <Value>-14179.040909261</Value>
              <InTolerance>True</InTolerance>
            </DeltaX>
            <DeltaY>
              <Value>-3572.6636230592</Value>
              <InTolerance>True</InTolerance>
            </DeltaY>
            <DeltaZ>
              <Value>-8319.8607607852</Value>
              <InTolerance>False</InTolerance>
            </DeltaZ>
          </ECEFDeltas>
    </PointRecord>
</Document>

测试时,我的 xml 文档有两个 PointRecords。程序的输出:

Point Record ID: 50 Time Stamp: 3/3/2017 9:39:54 AM
  Name: WF2510
  Code: EOC RECT 6/
  Method: StaticObservation
  Classication: Normal
  Deleted: False
  ECFDeltas-
    DeltaX: -14179.040909261 In Tolerance: True
    DeltaY: -3572.663623059 In Tolerance: True
    DeltaZ: -8319.860760785 In Tolerance: False
Point Record ID: 51 Time Stamp: 3/3/2017 9:39:54 AM
  Name: WF2510
  Code: EOC RECT 6/
  Method: StaticObservation
  Classication: Normal
  Deleted: False
  ECFDeltas-
    DeltaX: -14179.040909261 In Tolerance: True
    DeltaY: -3572.663623059 In Tolerance: True
    DeltaZ: -8319.860760785 In Tolerance: False

【讨论】:

  • 你使用了序列化,这对我来说是一个新概念。您的代码完全符合我的需要,因此我将仔细研究该技术模式。非常感谢!
  • 哦哦。可能是个问题。我没有提到这是一个 Windows 应用程序,而不是一个 Web 应用程序。我收到错误,因为它无法识别“文档”。
  • 我的是一个 Windows 控制台应用程序。 “document”是我的 xml 文件的名称,我把它放在我的代码旁边,在解决方案中,所以它在构建时被复制到输出目录。您应该将“document.xml”替换为您的 xml 文档的路径。
  • 或者你的意思是在这条线上Dim d As Document?然后你必须从你的 xml 生成你的类。有关详细信息,请参阅我的第一段中的链接。你应该在这样做之后定义这个类Partial Public Class Document...等等
  • 是的,这就是它显示错误的地方。现在已经过去了,但让其他人继续。我会尽快回复您。
猜你喜欢
  • 1970-01-01
  • 2017-11-29
  • 2015-05-22
  • 1970-01-01
  • 2013-04-15
  • 2011-09-09
  • 2013-03-22
  • 2020-06-02
  • 1970-01-01
相关资源
最近更新 更多