2009-09-04 59 views
3

如果我有存儲在數據庫,文件中的文本,資源並不重要:VB.NET XML文本解析

<code><%= Integer.MaxValue %></code> 

有沒有辦法做到這一點:

Dim el As XElement = XElement.Parse({variable containing text above}) 

,並有它評估表達式Integer.MaxValue?

回答

2

簡短的回答是否定的。編譯器知道如何解析它,並用你在C#代碼中看到的東西替換你的源代碼。

測試代碼:

Dim source As String = "a,s,d,f" 
Dim ar As String() = source.Split(","c) 
Dim el As XElement = <code> 
         <%= From s In ar Select s + "22" %> 
        </code> 

反射碼:

Dim VB$CG$t_i4$S0 As Integer 
Dim ar As String() = "a,s,d,f".Split(New Char() { ","c }) 
Dim VB$t_ref$S0 As New XElement(XName.Get("code", "")) 
VB$t_ref$S0.Add(ar.Select(Of String, String)(New Func(Of String, String)(AddressOf Test._Lambda$__1))) 
Dim el As XElement = VB$t_ref$S0 
+0

感謝您發佈最終答案。你介意說你是如何學到這些的? – 2009-09-04 20:14:17

+0

我用反射器反編譯我的程序集,看看編譯器用我的代碼做了什麼。你可以在http://www.red-gate.com/products/reflector/ – epitka 2009-09-04 20:17:07

1

不容易。最接近你可以使用CodeDOM生成一些代碼 - 你需要生成一個包裝模塊和一個帶有ReturnFunction,然後將表達式從數據庫中輸入到Return - 編譯它(並檢查編譯錯誤),運行它並查看結果。總而言之,這將非常緩慢。

+0

找到它你知道任何文章或資源,可以幫助人想要嘗試這樣的事情嗎? – CoderDennis 2010-01-30 06:45:03

+0

http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx涵蓋了C​​odeDOM。但是,它並沒有特別涵蓋XML文字,因爲這是一個VB特有的功能,所以您需要使用文字表達式 - http://msdn.microsoft.com/en-us/library/system.codedom。 codesnippetexpression.aspx – 2010-01-30 08:55:26