泛型和鸭打字XML在.NET?XML、NET

2023-09-03 06:56:44 作者:西山邪帝

我正在使用的数据实例的一些XML重新presentations。我使用.NET序列化的东西,但在我的灵魂受到干扰反序列化对象通过不必编写类重新present的XML ...下面是我喜欢做的事情,但我不知道是否语法或者如果它甚至有可能:

I'm working with some XML representations of data instances. I'm deserializing the objects using .NET serialization but something in my soul is disturbed by having to write classes to represent the XML... Below is what I'd LOVE to do but I don't know if the syntax or if it is even possible:

考虑以下几点:

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

xmlObject.SomePropertyDefinedInTheXML = SomeFunction()

与此有关途径的几点思考有什么建议?

Any suggestions on approachs with this?

推荐答案

VB.NET允许您使用XML在一个相当直观的方式:

VB.NET allows you to work with XML in a quite intuitive way:

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

缺点:你不必在这里强类型;在属性总是返回一个字符串。

Drawback: You don't have strong typing here; the Value property always returns a string.