首页 > 解决方案 > 泛化正则表达式 XML 验证

问题描述

我希望通过让例如Dim IDNumbers() As String = 'SOMETHING'而不是单独写出字符串列表来使我的代码更好{"0", "1", "2", "3", "4"}。我希望能够从我的 XML 文件中读取所有 ID、学生姓名和生日节点,而不必亲自去列出它们。我不确定如何做到这一点如果有人可以帮助提供示例代码,那就太好了。这样做的原因是,如果我修改文件中的代码,我也必须在我的 vb 代码中更改它。

标签: regexxmlvb.net

解决方案


Try following :

Imports System.Xml
Imports System.Xml.Linq
Imports System.Globalization
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim results = doc.Descendants("student").Select(Function(x) New With { _
                                                            .id = CType(x.Element("ID"), string), _
                                                            .name = CType(x.Element("student_name"), string), _
                                                            .birthday = CType(x.Element("birthday"), string) _
                                                        }).ToList()

    End Sub

End Module

推荐阅读