Search code examples
vb.neturixmlreader

visual basic 2010 express: Reading Xml file from public repository


I keep getting the following error when trying to parse data from tags from a public xml document.

XmlException was unhandled Cannot open 'http://datastore.unm.edu/events/events.xml'. The Uri parameter must be a relative path pointing to content inside the Silverlight application's XAP package. If you need to load content from an arbitrary Uri, please see the documentation on Loading XML content using WebClient/HttpWebRequest.

Here is the section of code I am editing:

Dim unmEventXml As XmlReader = XmlReader.Create("http://datastore.unm.edu/events/events.xml", New XmlReaderSettings())
Do While unmEventXml.Read()
    If unmEventXml.NodeType = XmlNodeType.Element AndAlso unmEventXml.Name = "summary" Then
        Me.Items.Add(New ItemViewModel() With {.LineOne = unmEventXml.Value, .LineTwo = "Maecenas praesent accumsan bibendum", .LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu"})
    Else
        unmEventXml.Read()
    End If

Solution

  • Have you read the exception message? That would be a good place to start :-)

    ... If you need to load content from an arbitrary Uri, please see the documentation on Loading XML content using WebClient/HttpWebRequest.

    So just use a WebClient to download the xml file:

    Dim client = New WebClient()
    Dim stream = client.OpenRead("http://datastore.unm.edu/events/events.xml")
    Dim unmEventXml = XmlReader.Create(stream)
    Do While unmEventXml.Read()
        ... Do your stuff ...
    Loop