本当に初心者の質問にお詫び申し上げます。私は、VBでXMLを使用することに非常に慣れており、批判的な理解に達するためにいくつかのガイダンスが必要です。XElementと.Elementを使用したXMLのクエリ
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Candy ID="MnMs">
<Form ID="tablet" Name="Tablet">
<Component ID="shell" Ingredients="sugar"/>
<Component ID="filling" Ingredients="chocolate"/>
</Form>
<Form ID="oblong">
<Component ID="shell" Ingredients="sugar"/>
<Component ID="filling" Ingredients="chocolate"/>
<Component ID="center" Ingredients="nut"/>
</Form>
</Candy>
<Candy ID="LBalls">
<Form ID="sphere">
<Component ID="shell" Ingredients="chocolate"/>
<Component ID="filling" Ingredients="chocolate ganache"/>
</Form>
</Candy>
<Candy ID="RPieces">
<Form ID="tablet">
<Component ID="shell" Ingredients="sugar"/>
<Component ID="filling" Ingredients="peanut butter ganache"/>
</Form>
</Candy>
</xs:schema>
(ID
の使用ではなく、推奨(より典型的に注意してください)id
?。)
VBで、私がアクセスする方法を、:様々なキャンディーを説明し、次のXMLを考える
Ingredients
の属性<Component>
RPieces/tablet/filling?具体的には、次の行:
<Component ID="filling" Ingredients="peanut butter ganache"/>
私のVB関数は次のとおりです。アイデンティティがどのように管理されているのか、特に属性がID
とid
であることを考えると、私は混乱しています。
Imports System.IO
Imports System.Xml
...
Function CandyFetch(ByVal candyId As String, ByVal formId As String, ByVal compId As String, ByVal attrId As String, Optional ByVal docPath As String = "Candies.xml") As String
Const ID = "ID"
Dim result = ""
docPath = docPath.Trim()
If Not File.Exists(docPath) Then docPath = AppDomain.CurrentDomain.BaseDirectory + docPath
For Each bonbon In XElement.Load(docPath).Elements("Candy")
If bonbon.Attribute(ID).Value = candyId Then
For Each form In bonbon.Elements("Form")
If form.Attribute(ID).Value = formId Then
For Each component In form.Elements("Component")
If component.Attribute(ID).Value = compId Then
result = component.Attribute(attrId).Value
Exit For
End If
Next
End If
Next
End If
Next
Return result
End Function
ありがとうございます。
ここでは、LinqToXmlを使ってこれを行う簡単な方法がありますが、XML要素を反復処理する必要はありませんか?
begi以下のようになります。 nner、here ... just Dim strXML =外部ファイルからテキストを読み込むFile.ReadAllText(docPath)? ...またはXDocument.Loadなどを使用する必要がありますか? – user1601638
'XmlDocument.Load'を使って、メモリ内の' String'を解析するのではなく、ファイルを読み込むことができます。私は私の答えを更新しました – SSS