asp.net – 如何使用Linq获取XML元素的值,即使是空的
请原谅我的愚蠢,我倾向于发现遍历的
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,我得到一个错误,说“对象引用未设置为对象的实例”.显然是因为该节点不存在. 我的问题是如何将值分配给变量,即使它是空的而不必在每次赋值之前进行条件检查? 解决方法
首先,myXMLFile.Descendants(“staff”).后代(“staffName”)是多余的.后代返回XDocument或XElement中任何级别的所有元素.因此,myXMLFile.Descendants(“staffName”)会给出相同的结果.
其次,您可以像这样使用Element属性和Value属性: staffBio = staff.Element("staffBio").Value 工作人员只有一个staffBio元素,因此不需要使用Descendants属性.值是一个字符串,因此您不需要调用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菜单中选择“创建模式”.) 有关更多信息和帮助,请查看“How Do I” video series on LINQ by Beth Massi. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 哪里可以将商业模式转换成模型?
- asp.net – 将可以为空的int绑定到asp:TextBox
- asp.net – 基于Web的可视化查询构建器
- 新版本 swagger 组件中 Servers 的 坑
- asp.net-mvc – 如何向ASP.NET MVC2控制器发送包含冒号的GE
- 在两个ASP.NET应用程序之间共享会话,其中一个嵌套在另一个A
- asp.net – 使用Ajax,在服务器或客户端生成额外的标记是否更
- 在ASP.NET 5 / Core 1.0开发期间使用完整的IIS
- asp.net – 在IIS 7.5上使用传出异步Web请求时的可扩展性问
- asp.net – 什么时候应该使用Page.DataBind()和Control.Dat