3

私は非常に単純な問題を抱えていますが、XML は初めてなので、いくつかの問題に直面しています。次の XML ドキュメントがあります。

<?xml version="1.0" encoding="utf-8"?>  
<Form_Layout>
  <Location>
    <LocX>100</LocX>
    <LocY>100</LocY>  
  </Location>  
  <Size>  
    <Width>300</Width>  
    <Height>300</Height>  
  </Size>  
</Form_Layout> 

私がやりたいことは、LocX、LoxY、Width、および Height 要素から対応する変数に値を読み取ることです。

これが私が試したことです:

Dim XmlReader = New XmlNodeReader(xmlDoc)  
While XmlReader.Read  
    Select Case XmlReader.Name.ToString()  
        Case "Location"  
            If XmlReader.??  
        Case "Size"  
            If XmlReader.??
    End Select  
End While  

しかし、各子ノードにアクセスする方法がわかりません。

4

4 に答える 4

3

Linq to XML を使用できる場合は、VB のXML Axis Propertiesを使用できます。

Dim root As XElement = XDocument.Load(fileName).Root

Dim LocX = Integer.Parse(root.<Location>.<LocX>.Value)
Dim LocY = Integer.Parse(root.<Location>.<LocY>.Value)

そしてroot.<Location>.<LocY>.Value = CStr(120)あまりにも動作します。

于 2012-07-22T10:00:15.203 に答える
0

LINQ-XML を使用し、

Dim root As XElement = XDocument.Load(fileName).Root

Dim LocX = Integer.Parse(root.Element("Location").Element("LocX").Value)
Dim LocY = Integer.Parse(root.Element("Location").Element("LocY").Value)
于 2012-07-20T15:21:57.527 に答える
0

各 XML ドキュメントには、最上部に宣言ノードが 1 つあります。宣言ノードは、xml ドキュメントに関する情報を提供します。

<?xml version="1.0" encoding="utf-8"?> 

ルート ノードは、宣言ノードの後に​​ xml ドキュメントに存在する必要があります。あなたの場合、ルートノードの名前は「Form_Layout」です。

<Form_Layout>

System.Xml 名前空間で使用可能なクラス オブジェクトを使用して、データを読み取って操作し、xml ファイルに保存できます。filestream オブジェクトを使用して、xml ファイルの内容全体を読み取ることができます。ノードを変更して、新しいファイルに保存するか、既存のファイルを上書きできます。次のコードを使用して、xml ファイルを XmlDataDocument オブジェクトに読み込むことができます。

    Dim xmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument
    Dim filepath As String = "C:\Users\mraso\Documents\location.xml"
    Dim loadedIn As Boolean = False, readbln As Boolean = False
    Dim fstream As System.IO.FileStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    If fstream IsNot Nothing Then
        xmlDoc.Load(fstream)
        loadedIn = True
    End If
    fstream.Close()
    fstream.Dispose()
    fstream = Nothing

XmlDataDocument オブジェクトの ChildNodes プロパティを使用して、xml ドキュメントの宣言ノードとルート ノードを読み取ることができます。また、各ノードの ChildNodes プロパティを呼び出して、その子が見返りとして記録されるようにすることもできます。XmlNodeList オブジェクト内の項目を反復処理して、個々のノードにアクセスできます。xml ファイルを操作するためのコード全体は次のとおりです。

Private Sub TestReadingAndSavingXml()
    Dim xmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument
    Dim filepath As String = "C:\Users\mraso\Documents\location.xml"
    Dim loadedIn As Boolean = False, readbln As Boolean = False
    Dim fstream As System.IO.FileStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    If fstream IsNot Nothing Then
        xmlDoc.Load(fstream)
        loadedIn = True
    End If
    fstream.Close()
    fstream.Dispose()
    fstream = Nothing
    Dim ndList1 As System.Xml.XmlNodeList = xmlDoc.ChildNodes
    If ndList1 IsNot Nothing Then
        Dim cnt As Integer = ndList1.Count
        For Each nd1 As System.Xml.XmlNode In ndList1
            If nd1.Name = "Form_Layout" Then
                Dim nd2 As System.Xml.XmlNode = GetChildNode(nd1, "Location")
                If nd2 IsNot Nothing Then
                    Dim nd3 As System.Xml.XmlNode = GetChildNode(nd2, "LocX")
                    If nd3 IsNot Nothing Then
                        Dim LocX_value As Integer = nd3.InnerText
                        Dim s = LocX_value
                    End If
                End If
            End If
        Next
    End If
    'Save data to a new xml file
    Dim outputXml As String = "C:\Users\mraso\Documents\location2.xml"
    xmlDoc.Save(outputXml)
End Sub

Function GetChildNode(ByRef node As System.Xml.XmlNode,
                      ByVal nodeName As String) As System.Xml.XmlNode
    If node IsNot Nothing Then
        Dim ndList1 As System.Xml.XmlNodeList = node.ChildNodes
        If ndList1 IsNot Nothing Then
            For Each nd1 As System.Xml.XmlNode In ndList1
                If nd1.Name = nodeName Then
                    Return nd1
                End If
            Next
        End If
    End If
    Return Nothing
End Function

上記のコードは、次の xml ファイルのノード "LocX" 内にある値 100 を読み取ります。

<?xml version="1.0" encoding="utf-8"?>  
<Form_Layout>
  <Location>
    <LocX>100</LocX>
    <LocY>100</LocY>  
  </Location>  
  <Size>  
    <Width>300</Width>  
    <Height>300</Height>  
  </Size>  
</Form_Layout>
于 2021-12-16T13:26:12.543 に答える
0

XmlDocumentXPathを使用してそれを行う方法を次に示します。LINQ と LINQを使用した例を自発的に提供してくれる人が他にもいると確信していますXDocument

Dim doc As New XmlDocument()
doc.LoadXml("...")
Dim locX As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Location/LocX").InnerText)
Dim locY As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Location/LocY").InnerText)
Dim width As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Size/Width").InnerText)
Dim height As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Size/Height").InnerText)

また、XmlSerializerクラスを調べて、興味があるかどうかを確認することもできます。そのクラスは XML ドキュメントを読み取り、それを使用して新しいオブジェクトのプロパティ値を設定します。逆シリアル化する XML の構造を模倣するクラスを作成するだけです。

于 2012-07-20T15:02:50.007 に答える