0

这是我在 VB.net 中的代码。它显示 AB、CD、EF。我如何制作一个子程序,以便我可以调用:Sub-name(field65, field547, field66)

Sub main()
    Dim fileName As String = "C:\AAA\test5.xml"
    Dim root As XElement = XDocument.Load(fileName).Root
    Dim a = root.<field65>.Value
    Dim b = root.<field547>.Value
    Dim c = root.<field66>.Value
    Console.WriteLine(a)
    Console.WriteLine(b)
    Console.WriteLine(c)
    Console.WriteLine("Please Press any key to continue!")
    Console.ReadKey()
End Sub
4

1 回答 1

0
Sub Foo(ByVal name1 As String, ByVal name2 As String, ByVal name3 As String)
    Dim fileName As String = "C:\AAA\test5.xml"
    Dim root As XElement = XDocument.Load(fileName).Root
    Dim a = root.Element(name1).Value
    Dim b = root.Element(name2).Value
    Dim c = root.Element(name3).Value
    Console.WriteLine(a)
    Console.WriteLine(b)
    Console.WriteLine(c)
    Console.WriteLine("Please Press any key to continue!")
    Console.ReadKey()

End Sub

应该做的,然后你可以调用Foo("field65", "field547", "field547").

但是使用

Sub Foo(ByVal name1 As XName, ByVal name2 As XName, ByVal name3 As XName)
    Dim fileName As String = "C:\AAA\test5.xml"
    Dim root As XElement = XDocument.Load(fileName).Root
    Dim a = root.Element(name1).Value
    Dim b = root.Element(name2).Value
    Dim c = root.Element(name3).Value
    Console.WriteLine(a)
    Console.WriteLine(b)
    Console.WriteLine(c)
    Console.WriteLine("Please Press any key to continue!")
    Console.ReadKey()

End Sub

更好,并且可以与命名空间一起使用。

于 2013-05-24T17:23:53.817 回答