我有一個 XML 檔案,我想使用包含命令搜索“val”的值并獲取其“索引”屬性以及“目錄名稱”屬性。
<list>
<catalog index="1" name="n1">
<val index="1">sample text 1</val>
<val index="2">sample text 2</val>
<val index="3">sample text 3</val>
</catalog>
<catalog index="2" name="n2">
<val index="1">sample text 0</val>
<val index="2">sample text 2</val>
<val index="3">sample text 3</val>
<val index="4">sample text 1</val>
<val index="5">sample text 5</val>
<val index="6">sample text 6</val>
</catalog>
<catalog index="3" name="n3">
<val index="1">sample text 8</val>
<val index="2">sample text 9</val>
<val index="3">sample text 10</val>
</catalog>
</list>
我用了
Dim xml_Doc = XDocument.Load(myPath & "list.Xml")
Dim search_result As IEnumerable(Of XElement)
search_result =
(From c In xml_Doc.Descendants("catalog")
Where c.Elements("val").Value.Contains("sample text 1")
Select c.Elements("val").Attributes("index").ToString & c.Attribute("name").Value)
我怎樣才能做到這一點?
輸出應如下所示:
index:1 , name: n1
index:4 , name: n2
uj5u.com熱心網友回復:
免責宣告:我喜歡 XmlSerialization。強型別和可重用物件。我將首先通過創建一些類來將檔案序列化為
Imports System.IO
Imports System.Xml.Serialization
<XmlRoot("list")>
Public Class List
<XmlElement("catalog")>
Public Property Catalogs As List(Of Catalog)
End Class
Public Class Catalog
<XmlElement("val")>
Public Property Vals As List(Of Val)
<XmlAttribute("index")>
Public Property Index As Integer
<XmlAttribute("name")>
Public Property Name As String
End Class
Public Class Val
<XmlAttribute("index")>
Public Property Index As Integer
<XmlText>
Public Property Text As String
End Class
然后反序列化檔案
Dim list As List
Dim serializer As New XmlSerializer(GetType(List))
Using sr As New StreamReader("list.xml")
list = CType(serializer.Deserialize(sr), List)
End Using
現在您的 xml 位于 .NET 類中,您可以使用 LINQ 來獲取您想要的內容
Dim searchString = "sample text 1"
Dim catalogs As New List(Of Catalog)()
For Each catalog In list.Catalogs
Dim vals = catalog.Vals.Where(Function(val) val.Text = searchString)
For Each val In vals
catalogs.Add(New Catalog() With {.Vals = {val}.ToList(), .Name = catalog.Name, .Index = catalog.Index})
Next
Next
Dim search_results = catalogs.SelectMany(Function(c) c.Vals.Select(Function(v) $"index:{v.Index}, name: {c.Name}"))
For Each search_result In search_results
Console.WriteLine(search_result)
Next
輸出:
索引:1,名稱:n1
索引:4,名稱:n2
它變得有點笨拙,因為您希望從 n 到 m 的關系(n 個目錄和 m 個 val)中取出 m 個專案,因此我們使用 SelectMany 將目錄投影到 val 的數量中。但是該物件list擁有您可以以任何方式用于其他目的的所有資料。
如另一個答案中所述,您正在搜索包含“示例文本 1”但回傳“示例文本 10”的字串,并且根據您所需的輸出,您應該搜索完全匹配。
uj5u.com熱心網友回復:
最簡單的方法是From對c.Elements("val")元素進行嵌套查詢,如下所示:
Dim sampleText = "sample text 1"
Dim search_result =
(From c In xml_Doc.Descendants("catalog")
From v in c.Elements("val")
Where v.Value.Contains(sampleText)
Select New With {.index = v.Attribute("index").Value, .name = c.Attribute("name").Value })
For Each s In search_result
Console.WriteLine("index:{0} , name:{1}", s.index, s.name)
Next
產生:
index:1 , name:n1
index:4 , name:n2
index:3 , name:n3
筆記:
這樣做可以讓您輕松過濾內部元素的值,
v然后從兩個v元素和外部元素中選擇屬性c。index:3 , name:n3包含在結果中,因為此元素的文本sample text 10包含搜索字串sample text 1。如果您不想包含此元素,請將您的Where子句更改為使用相等:Dim search_result = (From c In xml_Doc.Descendants("catalog") From v in c.Elements("val") Where v.Value = sampleText Select New With {.index = v.Attribute("index").Value, .name = c.Attribute("name").Value })
演示在這里和這里擺弄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359589.html
標籤:xml 网络 linq-to-xml
