2010-02-16 83 views
0

我使用XMLWriter手動創建XML文檔。有沒有辦法把這個字符串形式,所以我可以把它寫入我的數據庫?XML到字符串值

我編碼在VB.Net

+0

你到目前爲止的代碼將是一個好的開始。另外 - 你使用什麼數據庫系統? – Tomalak 2010-02-16 17:18:35

+0

http://stackoverflow.com/questions/955611/xmlwriter-to-write-to-a-string-instead-of-to-a-file – womp 2010-02-16 17:20:21

回答

5

你可以把它寫入StringBuilder

StringBuilder sb = new StringBuilder(): 
using (var writer = XmlWriter.Create(sb)) 
{ 
    // write the xml 
} 

string writtenXml = sb.ToString(); 

在VB.NET:

Dim sb As New StringBuilder() 

Using writer As XmlWriter = XmlWriter.Create(sb) 
    ' write the xml ' 
End Using 

Dim writtenXml As String = sb.ToString() 
+0

完美的謝謝! – 2010-02-16 17:26:11

0

你必須有你的XmlWriter緊裹StringWriter的。然後你就像你一樣處理它。當你想訪問字符串本身時調用StringWriter.ToString()。