How to: Load XML from a File, String, or Stream (Visual Basic)
You can create XML Literals and populate them with the contents from an external source such as a file, a string, or a stream by using several methods. These methods are shown in the following examples.
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
To load XML from a file
Dim books = XDocument.Load(My.Application.Info.DirectoryPath & "\..\..\Data\books.xml") Console.WriteLine(books)
To load XML from a string
Dim xmlString = "<Book id=""bk102"">" & vbCrLf & " <Author>Garcia, Debra</Author>" & vbCrLf & " <Title>Writing Code</Title>" & vbCrLf & " <Price>5.95</Price>" & vbCrLf & "</Book>" Dim xmlElem = XElement.Parse(xmlString) Console.WriteLine(xmlElem)
To load XML from a stream
Dim reader = System.Xml.XmlReader.Create(My.Application.Info.DirectoryPath & "\..\..\Data\books.xml") reader.MoveToContent() Dim inputXml = XDocument.ReadFrom(reader) Console.WriteLine(inputXml)