2017-03-10 3 views
0

必要に応じて、私はVB 2010のSystem.XMLの側面を詳しく調べています。フィールドの人たちが使用する機器によって生成された特定のXMLファイルを解析する際、タグ名とその内部テキストを抽出する。それは実際問題はありませんが、テキストボックスに結果をいくらかフォーマットされた形で提示するために、各子ノードの子孫のレベルを決定する必要があります。例えば、以下のXMLコードから:VB 2010 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 

私が抱えている問題は、各子孫ノードの子ノードから名前と値をインデントするために、または2つのスペース、4つのスペースを入れるタイミングを知るために、サブレベルの各ノードに含まれる結果をグループ化する方法を知ることです私は各ノードの子孫のレベルを示す整数値を見つけようとしていますが、私は運がありません。

誰かが助けることができますか?

ありがとうございます!

+0

xmlドキュメントは使用可能なノードの良い例ですか?あるいは、多かれ少なかれノードと子孫レベルが存在する可能性がありますか? – djv

+0

ファイルには何百ものノードがありますが、特定の属性を持つノードのみを選択しています。しかし、これらのノードにはいくつかのレベルの子孫があるため、子孫のレベルは兄弟から兄弟に変化します。私は現在それに近づいているので、可能な限り多くのレベルまでドリルダウンするための「for-each」ループがネストされていますが、どれくらいのドリルをしてドリルを停止するのかを知ることは難しいです。言い換えれば、どのくらい入れ子にするか。あなたの興味に感謝します。私はこれを正しく説明してくれることを願う。 –

+0

xmlを読むためにどの技術を使用しているのかよくわかりません。私は多くのXML文書を使用しますが、それらは常に標準モデルに従います。あなたが提供したモデルに基づいて回答を投稿します。それはあなたのために働くかもしれません(しかし、あなたの要件に基づいてより多くのモデルを生成する可能性があります)。私の答えを見てください。 – djv

答えて

0

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 

私はあなたがこれを持っていると仮定すると、PointRecordの配列でモデルを生成するために別の<PointRecord ID="00000050" TimeStamp="2017-03-03T09:39:54">ノードを追加しました。

<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文書には2つの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 
+0

私は新しいコンセプトのシリアライゼーションを使用しました。あなたのコードは、私が必要とするものとまったく同じですから、私はそのテクニックモードを綿密に勉強します。本当にありがとう! –

+0

あああ。問題になるかもしれません。私はこれがWindowsアプリケーションであり、Webアプリケーションではないことに言及しませんでした。 「ドキュメント」を認識しないため、エラーが発生しています。 –

+0

MineはWindowsコンソールアプリケーションです。 "document"は私のxmlファイルの名前です。ソリューションの中に私のコードのそばに置くので、ビルド時に出力ディレクトリにコピーされます。 "document.xml"をxmlドキュメントのパスに置き換える必要があります。 – djv

関連する問題