Convert linq.Xelement to stream for XMLTextReader

I am creating 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 uri using 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 the stream instead, but my efforts have so far come to naught. (Is this the best practice?)

I use NMock2 for bullying if I have to do something about it.

+4
source share
2 answers

Instead of pushing the XmlTextReader through the stream, if you just do not use XmlReader , you can simply use XNode.CreateReader . This is a much simpler approach than saving to a stream, unless your API forces you to use a stream or XmlTextReader .

+8
source

xmlFile is an XDocument that can be saved to a MemoryStream , see the following SO question for details:

You can then force your method to accept a generic Stream , which can be a MemoryStream (in unit test) or FileStream .

0
source

All Articles