Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert linq.Xelement to stream for XMLTextReader

I am producing an XML file in my unit test using

Public Sub rssParserTest
   Dim Const rssUri as String = "rssTestFile.xml"
   Dim xmlFile = <rss version="2.0">
   ...
                 </rss>
   xmlFile.save(rssUri)

  rssParser(rssUri)
End Sub

and consuming the uri with an XMLTextReader

Public Sub rssParser(ByVal rssUri as string)
    Dim rssXml = New XmlTextReader(rssUri)
    rssXml.read
    ...
End Sub

I want to remove the unit test dependency on a physical file and use a stream instead but my efforts so far have come to nought. (Is this best practise?)

I am using NMock2 for mocking if I should be doing something with that.

like image 561
Bender Avatar asked Nov 16 '10 20:11

Bender


1 Answers

Rather than force an XmlTextReader via a stream, if you just nead an XmlReader you can just use XNode.CreateReader. That's a much simpler approach than saving to a stream, unless your API forces you to use a stream or an XmlTextReader.

like image 158
Jon Skeet Avatar answered Nov 16 '22 19:11

Jon Skeet