From: Oleg T. <he...@us...> - 2005-10-25 21:33:54
|
Update of /cvsroot/mvp-xml/Common/v2/test/XPathIteratorReaderTests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1205/v2/test/XPathIteratorReaderTests Added Files: Tests.cs rss.xml Log Message: --- NEW FILE: Tests.cs --- using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; using System.Xml.Schema; using NUnit.Framework; using Mvp.Xml.Common.XPath; namespace Mvp.Xml.Tests.XPathIteratorReaderTests { [TestFixture] public class Tests { [Test] public void TestRss() { string theWord = "XML"; XPathDocument doc = new XPathDocument(Globals.GetResource( this.GetType().Namespace + ".rss.xml")); XPathNodeIterator it = doc.CreateNavigator().Select( "/rss/channel/item[contains(title,'" + theWord + "')]"); XPathIteratorReader reader = new XPathIteratorReader(it); reader.MoveToContent(); string xml = reader.ReadOuterXml(); using (StreamWriter sw = new StreamWriter(@"c:\subset.xml", false)) { XmlTextWriter tw = new XmlTextWriter(sw); tw.WriteNode(new XPathIteratorReader(it), false); tw.Close(); } Assert.IsTrue(xml != String.Empty); } [Test] public void Test1() { XPathDocument doc = new XPathDocument(Globals.GetResource(Globals.PubsResource)); XPathNodeIterator it = doc.CreateNavigator().Select("//price[text() < 5]"); XPathIteratorReader reader = new XPathIteratorReader(it, "prices"); reader.MoveToContent(); string xml = reader.ReadOuterXml(); Assert.IsTrue(xml != String.Empty); } [Test] public void FunctionalTest() { XPathDocument doc = new XPathDocument(new StringReader( "<customer xmlns='mvp-xml'><order id='1'/><order id='2'/><order id='5'/></customer>")); XPathNavigator nav = doc.CreateNavigator(); XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("mvp", "mvp-xml"); // On purpose, the query is wrong because it doesn't use the prefix. XPathExpression expr = nav.Compile("//order[@id < 3]"); expr.SetContext(mgr); XPathNodeIterator it = nav. Select(expr); XPathIteratorReader reader = new XPathIteratorReader(it, "orders"); reader.MoveToContent(); string xml = reader.ReadOuterXml(); Assert.AreEqual("<orders></orders>", xml); // With the right query now. expr = nav.Compile("//mvp:order[@id < 3]"); expr.SetContext(mgr); it = nav.Select(expr); reader = new XPathIteratorReader(it, "orders"); reader.MoveToContent(); xml = reader.ReadOuterXml(); Assert.AreEqual("<orders><order id=\"1\" xmlns=\"mvp-xml\" /><order id=\"2\" xmlns=\"mvp-xml\" /></orders>", xml); } } } --- NEW FILE: rss.xml --- <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>XML</title><link>http://weblogs.asp.net/cazzu/category/1567.aspx</link><description>XML</description><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.111</generator><item><dc:creator>Daniel Cazzulino</dc:creator><title>Mvp.Xml project: healthy start</title><link>http://weblogs.asp.net/cazzu/archive/2004/04/25/119940.aspx</link><pubDate>Sun, 25 Apr 2004 19:01:00 GMT</pubDate><guid>http://weblogs.asp.net/cazzu/archive/2004/04/25/119940.aspx</guid><wfw:comment>http://weblogs.asp.net/cazzu/comments/119940.aspx</wfw:comment><comments>http://weblogs.asp.net/cazzu/archive/2004/04/25/119940.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/cazzu/comments/commentRss/119940.aspx</wfw:commentRss><trackback:ping>http://weblogs.asp.net/cazzu/services/trackbacks/119940.aspx</trackback:ping><description>I've just checked the stats of our <a href="http://sourceforge.net/projects/mvp-xml">Mvp.Xml</a> project. Pretty good news for a project born only two months ago: <p> Page views: 1.421<br/> Downloads: 243<br/> Last 30 days we've had a steady rate of increase in page views of 200% a day, and 150% in downloads.</p><img src ="http://weblogs.asp.net/cazzu/aggbug/119940.aspx" width = "1" height = "1" /></description></item><item><dc:creator>Daniel Cazzulino</dc:creator><title>Reading XML fragments with XmlTextReader</title><link>http://weblogs.asp.net/cazzu/archive/2004/04/23/119263.aspx</link><pubDate>Fri, 23 Apr 2004 21:51:00 GMT</pubDate><guid>http://weblogs.asp.net/cazzu/archive/2004/04/23/119263.aspx</guid><wfw:comment>http://weblogs.asp.net/cazzu/comments/119263.aspx</wfw:comment><comments>http://weblogs.asp.net/cazzu/archive/2004/04/23/119263.aspx#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://weblogs.asp.net/cazzu/comments/commentRss/119263.aspx</wfw:commentRss><trackback:ping>http://weblogs.asp.net/cazzu/services/trackbacks/119263.aspx</trackback:ping><description> <p>Back at the <a href="http://mvp.support.microsoft.com/default.aspx?scid=fh;en-us;mvpsmt2004"> 2004 MVP Global Summit</a>, I met fellow XML fan <a href="http://weblogs.asp.net/kaevans"> Kirk</a>, who was seeking a solution to the following problem: you have a (several) megabytes file containing multiple XML fragments, and you want to read it (in <a href="http://weblogs.asp.net/kaevans/archive/2004/04/24/119508.aspx">his case</a> specially through the <a href="http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=B90FDDCE-E60D-43F8-A5C4-C3BD760564BC">SgmlReader</a>). The problem is, of course, that the <code>XmlTextReader</code> will throw an exception as soon as it finds the second fragment, unless you use the special ctor. overload that takes an <code>XmlParsingContext</code>.<a href="http://www.25hoursaday.com/"> Dare</a> <a href="http://msdn.microsoft.com/xml/default.aspx?pull=/library/en-us/dnxmlnet/html/largexml.asp"> shows</a> an alternate solution based on XML inclusion techniques, either DTD external entities or <a href="http://workspaces.gotdotnet.com/xinclude">XInclude</a>. </p> <P>These techniques effectively expose a fully well-formed document to your application, which has a number of benefits, including the ability to transform it if you need to, for example. But I was thinking more along the lines of providing a class that could actually read the fragments without resorting to those mechanisms. I couldn't cheat the <code>XmlTextReader</code>, so I decided to go one step lower. The result is the <code>XmlFragmentStream</code>, a class that wraps any <code>System.IO.Stream</code> and fakes the missing root element, so that an <code>XmlTextReader</code> layered on top of it, will think [...2270 lines suppressed...] } } /// <summary> /// Advances the iteration cursor. /// </summary> /// <returns>True if more nodes remain in the iterator.</returns> bool IEnumerator.MoveNext() { return _iterator.MoveNext(); } #endregion } #endregion }</xmp> </div> <p><strong>Update: check an even better approach <a href="http://weblogs.asp.net/cazzu/archive/2004/04/14/113479.aspx">here</a>.</strong></p> <p>Enjoy!</p><img src ="http://weblogs.asp.net/cazzu/aggbug/86609.aspx" width = "1" height = "1" /></description></item></channel></rss> |