.NET中的泛型和鸭子打字XML?

Ach*_*les 7 .net vb.net asp.net generics duck-typing

我正在使用一些数据实例的XML表示.我使用.NET序列化的东西,但在我的灵魂受到干扰反序列化对象的需要编写类来表示的XML ...下面是我很想这样做,但我不知道是不是语法,或者如果它甚至可能:

考虑以下:

dim xmlObject = SomeXMLFunction() 'where some function returns an object/string representation of xml...

xmlObject.SomePropertyDefinedInTheXML = SomeFunction()
Run Code Online (Sandbox Code Playgroud)

有关此方法的任何建议吗?

Hei*_*nzi 3

VB.NET 允许您以非常直观的方式使用 XML:

Sub Serialize()
    Dim xml = <myData>
                  <someValue><%= someFunction() %></someValue>
              </myData>
    xml.Save("somefile.xml")
End Sub

Sub Serialize2()   ' if you get the XML skeleton as a string
    Dim xml = XDocument.Parse("<myData><someValue></someValue></myData>")
    xml.<myData>.<someValue>.Value = "test"
    xml.Save("somefile.xml")
End Sub

Sub Deserialize()
    Dim xml = XDocument.Load("somefile.xml")

    Dim value = xml.<myData>.<someValue>.Value
    ...
End Sub
Run Code Online (Sandbox Code Playgroud)

缺点:这里没有强类型;该Value属性始终返回一个字符串。