簡體   English   中英

從嵌入式資源讀取XML文件的最佳方法是什么?

[英]What is the best way to read an XML file from Embedded resources?

Dim xmldoc As New XmlDocument()
Dim xmlnode As XmlNodeList
Dim i As Integer
Dim str As String

xmldoc.LoadXml("countyByRegion.xml")

這拋出

System.Xml.dll中發生類型'System.Xml.XmlException'的異常,但未在用戶代碼中處理。 附加信息:根級別的數據無效。 第1行,位置1。

LoadXml不用於加載文件,而是用於將XML作為字符串加載。 因此,它希望您執行類似LoadXml("<root><bar>hi</bar></root>"); 顯然,文件名不是有效的XML。

您可以使用“ Load加載文件。 您將像現在使用LoadXml一樣使用它,只需指定文件的路徑即可。

我通常會這樣做

Dim fsXML As New FileStream("countyByRegion.xml", FileMode.Open, FileAccess.Read)

然后使用xmldoc.Load(fsXML)

 Dim fileText As String
    Dim a As Assembly = GetType(ZP957_Form).Assembly
    Using reader As New StreamReader(a.GetManifestResourceStream("ZP957.countyByRegion.xml"))
        fileText = reader.ReadToEnd()
    End Using

    xmldoc.LoadXml(fileText)

由於xml是嵌入式的,因此必須從程序集中讀取它,因為我必須使用GetManifestResourceStream,然后可以執行LoadXml,因為現在有了xml的文件流。 這對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM