请原谅我的愚蠢,我倾向于发现遍历的XML过于复杂。
我在VB中使用ASP.NET。
我有一份XML文档,其中包含我公司员工的所有详细信息......
<staff>
<staffName>Test Staff</staffName>
<staffTitle>Slave</staffTitle>
<staffDepartmentName>Finance</staffDepartmentName>
<staffOffice>London</staffOffice>
<staffEmail>t.staff@company.co.uk</staffEmail>
<staffPhone>0207 123 456</staffPhone>
<staffNotes>Working hours Mon to Thurs 9.15 - 5.15</staffNotes>
<staffBio></staffBio>
</staff>
正如您所看到的,某些节点并不总是包含任何员工的数据;只有董事才有传记。
我访问像这样的值......
For Each staff In ( _
From matches In myXMLFile.Descendants("staff").Descendants("staffName") _
Where matches.Nodes(0).ToString.ToLower.Contains(LCase(search)) _
Order By matches.Value _
Select matches)
staffName = staff.Descendants("staffName").Nodes(0).ToString)
staffTitle = staff.Descendants("staffTitle").Nodes(0).ToString)
staffOffice = staff.Descendants("staffOffice").Nodes(0).ToString)
staffEmail = staff.Descendants("staffEmail").Nodes(0).ToString)
staffPhone = staff.Descendants("staffPhone").Nodes(0).ToString)
staffNotes = staff.Descendants("staffNotes").Nodes(0).ToString)
staffBio = staff.Descendants("staffBio").Nodes(0).ToString)
' Do something with that data...
Next
一旦它到达staffBio,我得到一个错误,说“对象引用未设置为对象的实例”。显然是因为该节点不存在。
我的问题是如何将值分配给变量,即使它是空的而不必在每次分配之前进行条件检查?
答案 0 :(得分:4)
首先,myXMLFile.Descendants("staff").Descendants("staffName")
是多余的。后代返回XDocument
或XElement
中任何级别的所有元素。因此,myXMLFile.Descendants("staffName")
会得到相同的结果。
其次,你可以像这样使用Element属性和Value属性:
staffBio = staff.Element("staffBio").Value
staff
只有一个staffBio
元素,因此无需使用Descendants属性。 Value
是一个字符串,因此您无需致电Value.ToString
。如果元素为空,则Value将返回一个空字符串,这正是您要查找的内容!
第三,在VB.NET中有一个更好的(我相信更直接)的方式。这是一个控制台应用程序,演示了我将如何做到这一点:
Module Module1
Sub Main()
Dim myXMLFile = <allStaff>
<staff>
<staffName>Test Staff</staffName>
<staffTitle>Slave</staffTitle>
<staffDepartmentName>Finance</staffDepartmentName>
<staffOffice>London</staffOffice>
<staffEmail>t.staff@battens.co.uk</staffEmail>
<staffPhone>0207 123 456</staffPhone>
<staffNotes>Working hours Mon to Thurs 9.15 - 5.15</staffNotes>
<staffBio></staffBio>
</staff>
<staff>
<staffName>Other Staff</staffName>
<staffTitle>Master</staffTitle>
<staffDepartmentName>IT</staffDepartmentName>
<staffOffice>Oxford</staffOffice>
<staffEmail>o.staff@battens.co.uk</staffEmail>
<staffPhone>0207 123 789</staffPhone>
<staffNotes></staffNotes>
<staffBio>Some guy.</staffBio>
</staff>
</allStaff>
Dim search = "Test"
Dim searchQuery = From staff In myXMLFile...<staff> _
Where staff.<staffName>.Value.Contains(search) _
Select si = New StaffInfo With {.Name = staff.<staffName>.Value, _
.Title = staff.<staffTitle>.Value, _
.Department = staff.<staffDepartmentName>.Value, _
.Office = staff.<staffOffice>.Value, _
.Email = staff.<staffEmail>.Value, _
.Phone = staff.<staffPhone>.Value, _
.Notes = staff.<staffNotes>.Value, _
.Bio = staff.<staffBio>.Value}
For Each staff In searchQuery
Console.WriteLine("Name: {0}", staff.Name)
Console.WriteLine("Title: {0}", staff.Title)
Console.WriteLine("Department: {0}", staff.Department)
Console.WriteLine("Office: {0}", staff.Office)
Console.WriteLine("Email: {0}", staff.Email)
Console.WriteLine("Phone: {0}", staff.Phone)
Console.WriteLine("Notes: {0}", staff.Notes)
Console.WriteLine("Bio: {0}", staff.Bio)
Console.WriteLine()
Next
Console.ReadLine()
End Sub
Private Class StaffInfo
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _title As String
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Private _department As String
Public Property Department() As String
Get
Return _department
End Get
Set(ByVal value As String)
_department = value
End Set
End Property
Private _office As String
Public Property Office() As String
Get
Return _office
End Get
Set(ByVal value As String)
_office = value
End Set
End Property
Private _email As String
Public Property Email() As String
Get
Return _email
End Get
Set(ByVal value As String)
_email = value
End Set
End Property
Private _phone As String
Public Property Phone() As String
Get
Return _phone
End Get
Set(ByVal value As String)
_phone = value
End Set
End Property
Private _notes As String
Public Property Notes() As String
Get
Return _notes
End Get
Set(ByVal value As String)
_notes = value
End Set
End Property
Private _bio As String
Public Property Bio() As String
Get
Return _bio
End Get
Set(ByVal value As String)
_bio = value
End Set
End Property
End Class
End Module
如果您有XML的架构(.xsd文件),那么您可以将对该xmlns的引用导入VB源文件,这将为您提供用于编写LINQ to XML查询的智能感知。
(编辑:创建架构的一种快捷方法是在Visual Studio中打开XML文件,然后从XML菜单中选择Create Schema。)
有关详细信息和帮助,请查看"How Do I" video series on LINQ by Beth Massi。
答案 1 :(得分:0)
好吧,想想这是怎么做的。
...
staffBio = staff.Descendants("staffBio").ElementAtOrDefault(0).Value.ToString)
...
使用.ElementAtOrDefault(0)
代替.Nodes(0)
只返回“”,如果它为空,或<staffBio>whatever</staffBio>
如果不为。
.Value只返回标签的内容,如上例中的“what”。
这是对的吗?任何人都可以看到这个问题吗?