Friday, August 27, 2004

XML includes and getting data from external source.

When using XMLTextReader ever wondered how to read the include files. Recently I was stuck with a similar problem after much head scratching finally resolved it. Here is the code.
Ok I was trying to read an xsd schema and every time it included schemalocation I would get stuck so I decided to resolve it using  getEnity method.
 

Dim vreader As New XIncludingReader(" the url where the schema is")

Dim resolver As New XmlUrlResolver

Dim i As Integer = 0

Dim fur As Uri

Dim s As Stream

Dim xtext As StreamReader

Dim st As String

 

vreader.XmlResolver = resolver

     

If (vreader.Name = "schemaLocation") Then

   fur = resolver.ResolveUri(New Uri("….location"), vreader.Value)

   If Not fur Is Nothing Then

      ‘Ok going to get the data from the location

      ‘the beauty of this thing is if your location is on the file or in a url

      ‘it will resolve it fine.

      s = resolver.GetEntity(fur, Nothing, GetType(Stream))

      xtext = New StreamReader(s)

      st = xtext.ReadToEnd

      Console.WriteLine(st)

   End If

End If

 

No comments:

Post a Comment