XmlDocumentオブジェクトの作成

メモ:  Category:vb

ローカルのXMLファイルやインターネット上のXMLファイルを操作するためXML文書からXmlDocumentオブジェクトを作成します。

XML文書からXmlDocumentオブジェクトを作成するには、Load関数又はLoadXml関数を使用します。

XmlDocument.Load
ディスク上のパスの指定やストリーム(Stream,TextReader,XmlReader)からオブジェクトを作成します。
XmlDocument.LoadXml
メモリ上(String型)からオブジェクトを作成します。

サンプルとしてlivedoorのお天気情報サービス(Livedoor Weather Web Service / LWWS)を使用しています。

http://weather.livedoor.com/forecast/webservice/rest/v1?city=84&day=today

Load関数をする場合

URLを指定する

パスの変わりにURLを指定することもできます。

Dim weatherxml As New XmlDocument

weatherxml.Load("d:\v1.xml")

WebClientを使ってインターネット上のファイルを指定する

Dim web As New System.Net.WebClient()
Dim st As System.IO.Stream = web.OpenRead("http://weather.livedoor.com/forecast/" _
                           "webservice/rest/v1?city=84&day=today")

weatherxml.Load(st)

st.Close()

WebClientを使ってインターネット上のファイルを指定する

Dim weatherxml As New XmlDocument
Dim req As System.Net.HttpWebRequest

req = System.Net.HttpWebRequest.Create("http://weather.livedoor.com/forecast/" _
                           "webservice/rest/v1?city=84&day=today")

Dim res As System.Net.HttpWebResponse = req.GetResponse()

weatherxml.Load(res.GetResponseStream())

LoadXml関数をする場合

メモリ上のXMLを指定する

Dim weatherxml As New XmlDocument
Dim strXml As String = "<item><name>wrench</name></item>"

weatherxml.LoadXml(strXml)

bluenote by BBB