Linq to xml 组合值
Linq to xml combine values
考虑以下 MediaObject class
Public Class MediaObject
Public Path as String
Public File as String
Public Sub New(_path as String, _file as String)
Path = _path
File = _file
End Sub
End Class
我有以下 XML(我的xml):
<records>
<media>
<path>\first\path</path>
<file>firstfile</file>
</media>
<media>
<path>\second\path</path>
<file>secondfile</file>
</media>
<records>
要获取 MediaObjects 列表,我使用这个:
Dim mobjects As New List(Of MediaObject)
Dim x As XDocument = XDocument.Parse(myxml)
mobjects = (From m In x.<records>.<media> Select media = New MediaObject(m.<path>.Value, m.<file>.Value)).ToList()
一切都很好。
但是现在考虑这个新的XML(第二个文件是第一个文件的替代):
<records>
<media>
<path>\first\path</path>
<file>firstfile</file>
<path>\second\path</path>
<file>secondfile</file>
</media>
</records>
我可以轻松获得其中一个属性,但不能同时获得两个属性,即
Dim mobjects As New List(Of MediaObject)
Dim x As XDocument = XDocument.Parse(myxml)
'here get only the paths
Dim r = (From m In x.<records>.<media> Select media = (From t In m.<path> Select New MediaObject(t.Value, Nothing)).ToList()).ToList()
mobjects = r(0)
在此上下文中我将如何创建 MediaObjects 列表?
(让我们考虑 xml 中的路径和文件值是按顺序排列的,并且是 2 乘 2)
谢谢!
更新:
对不起,我不够精确。
这是真实世界的场景。
可以有很多路径和文件,并且保证
- 路径在文件之前
- 路径和文件的数量相同
- 首先是所有路径,然后是所有文件
样本:
<records>
<media>
<some_tags />
<path>\first\path</path>
<path>\second\path</path>
<might_be_something_here />
<file>firstfile</file>
<file>secondfile</file>
<more_tags />
</media>
</records>
PS: 我无法更改来自另一个系统的 XML...
假设您只是将第 n 个路径和文件配对,您可以这样做:
Dim paths = doc...<path>
Dim files = doc...<file>
Dim query = paths.Zip(files, Function(p, f)
New MediaObject(CType(p, String), CType(f, String))
)
您甚至不必担心介于两者之间的可能元素,它们将被忽略。
考虑以下 MediaObject class
Public Class MediaObject
Public Path as String
Public File as String
Public Sub New(_path as String, _file as String)
Path = _path
File = _file
End Sub
End Class
我有以下 XML(我的xml):
<records>
<media>
<path>\first\path</path>
<file>firstfile</file>
</media>
<media>
<path>\second\path</path>
<file>secondfile</file>
</media>
<records>
要获取 MediaObjects 列表,我使用这个:
Dim mobjects As New List(Of MediaObject)
Dim x As XDocument = XDocument.Parse(myxml)
mobjects = (From m In x.<records>.<media> Select media = New MediaObject(m.<path>.Value, m.<file>.Value)).ToList()
一切都很好。 但是现在考虑这个新的XML(第二个文件是第一个文件的替代):
<records>
<media>
<path>\first\path</path>
<file>firstfile</file>
<path>\second\path</path>
<file>secondfile</file>
</media>
</records>
我可以轻松获得其中一个属性,但不能同时获得两个属性,即
Dim mobjects As New List(Of MediaObject)
Dim x As XDocument = XDocument.Parse(myxml)
'here get only the paths
Dim r = (From m In x.<records>.<media> Select media = (From t In m.<path> Select New MediaObject(t.Value, Nothing)).ToList()).ToList()
mobjects = r(0)
在此上下文中我将如何创建 MediaObjects 列表? (让我们考虑 xml 中的路径和文件值是按顺序排列的,并且是 2 乘 2)
谢谢!
更新: 对不起,我不够精确。 这是真实世界的场景。 可以有很多路径和文件,并且保证
- 路径在文件之前
- 路径和文件的数量相同
- 首先是所有路径,然后是所有文件
样本:
<records>
<media>
<some_tags />
<path>\first\path</path>
<path>\second\path</path>
<might_be_something_here />
<file>firstfile</file>
<file>secondfile</file>
<more_tags />
</media>
</records>
PS: 我无法更改来自另一个系统的 XML...
假设您只是将第 n 个路径和文件配对,您可以这样做:
Dim paths = doc...<path>
Dim files = doc...<file>
Dim query = paths.Zip(files, Function(p, f)
New MediaObject(CType(p, String), CType(f, String))
)
您甚至不必担心介于两者之间的可能元素,它们将被忽略。