泛型和鸭打字XML在.NET? [英] Generics and Duck-Typing XML in .NET?

查看:133
本文介绍了泛型和鸭打字XML在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的数据实例的一些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.

这篇关于泛型和鸭打字XML在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆