开发者

Convert linq.Xelement to stream for XMLTextReader

开发者 https://www.devze.com 2023-01-25 16:09 出处:网络
I am producing an XML file in my unit test using Public Sub rssParserTest Dim Const rssUri as String = \"rss开发者_Go百科TestFile.xml\"

I am producing an XML file in my unit test using

Public Sub rssParserTest
   Dim Const rssUri as String = "rss开发者_Go百科TestFile.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.


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.


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

  • Convert XDocument to Stream (The code samples are in C#, but the .NET API methods used are the same.)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号