Bug in MSXML / XML Parser .Net

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Michael (_at_discussions.microsoft.com)
Date: 10/06/04


Date: Wed, 6 Oct 2004 05:07:01 -0700

Hi,

I have a problem parsing XML file using XSLT style*** by using :

using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

// load Xsl style***
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(Server.MapPath("style***.xslt"));

// load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(Server.MapPath("file.xml"));

// write the transformed result
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("result.xml"), null);

// do the actual transform of Xml
// pass XmlResolver to Transform() method
myXslTrans.Transform(myXPathDoc, null, writer, null);
writer.Close();

XML file

<?xml version="1.0" encoding="utf-8"?>

<Documents>
   <Document chapter="1" title="title 1" href="file1.xml" filter="">
      <Article title="1.1" info="sub" filter="food"/>
      <Article title="1.2" info="main" filter="food"/>
   </Document>
   <Document chapter="2" title="title 2" href="file2.xml" filter="drink">
      <Article title="2.1" info="sub" filter="drink"/>
      <Article title="2.2" info="main" filter="food"/>
   </Document>
</Documents>

XSLT style***

<?xml version='1.0' encoding='UTF-8'?>

<xsl:style*** version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <!-- using muenchian method group by an attribute by asigning a unique
key to each element -->
    
   <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  
   <!-- xsl:key name="by-proctype" match="Doc" use="@proceduretype"/ -->
    
   <!-- define info key -->
   <xsl:key name="by-info" match="Article" use="@info"/>
    
   <!-- put the filter string in a global parameter -->
  <xsl:param name="filter" select="'food'"/>
    
  <xsl:template match="Documents"><!-- @filter='' will be true if it is
there and empty or if it is not there at all) -->

   <documents>
      <xsl:for-each select="Document[@filter='' or
@filter=$filter]/Article[count(.|key('by-info',@info)[@filter='' or
@filter=$filter][1])=1]">
         <document name="{@info}"><xsl:copy-of
select="key('by-info',@info)[@filter=$filter]"/></document>
      </xsl:for-each>
   </documents>
</xsl:template>
  
</xsl:style***>

Exception Details:

System.NullReferenceException: Object reference not set to an instance of an
object."

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
  System.Xml.XPath.FilterQuery.SetXsltContext(XsltContext input)
  System.Xml.XPath.FilterQuery.SetXsltContext(XsltContext input)
  System.Xml.XPath.MergeFilterQuery.SetXsltContext(XsltContext input)
  System.Xml.XPath.OrQuery.SetXsltContext(XsltContext context)
  System.Xml.XPath.MethodOperand.SetXsltContext(XsltContext context)
  System.Xml.XPath.LogicalExpr.SetXsltContext(XsltContext context)
  System.Xml.XPath.FilterQuery.SetXsltContext(XsltContext input)
  System.Xml.XPath.MergeFilterQuery.SetXsltContext(XsltContext input)
  System.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager
nsManager)
  System.Xml.Xsl.Processor.GetCompiledQuery(Int32 key)
  System.Xml.Xsl.Processor.StartQuery(XPathNavigator context, Int32 key)
  System.Xml.Xsl.ForEachAction.Execute(Processor processor, ActionFrame
frame)
  System.Xml.Xsl.ActionFrame.Execute(Processor processor)
  System.Xml.Xsl.Processor.Execute()
  System.Xml.Xsl.XslTransform.Transform(XPathNavigator input,
XsltArgumentList args, XmlWriter output, XmlResolver resolver)
  System.Xml.Xsl.XslTransform.Transform(IXPathNavigable input,
XsltArgumentList args, XmlWriter output, XmlResolver resolver)
  com.pop.mm_main_v2.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\file\index.aspx.cs:61
.....

When I use XMLSpy to parse the XML using MSXML 4 processor it works perfect.
Saxon works perfectly as well. But when I try to parse it trough C#, I get
the error above.

It looks like I don't use MSXML 4 at all.

How do I make sure that MSXML 4 is used when I parse?

Thank you,

-Mike


Quantcast