473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What kind of errors to expect in XSLT using C#?

Hi all,

I am writing a code snippet for a website in ASP.NET

I need to accept an inputXML in memorystream object, and perform XSLT on it.

If the transform is successful, I need to output the converted XML in stream, otherwise, display the errors encountered.

I have started learning .NET recently, so I am not very well versed with it.

What kind of errors should I expect and how do I catch them?

Any suggestions to improve my code will be appreciated.

Thanks.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Xml;
  9. using System.Xml.Xsl;
  10. using System.Text;
  11. using System.Xml.XPath;
  12. using System.Diagnostics;
  13.  
  14.  
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.  
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.         XmlReader xml = LoadXML();
  21.         XmlReader xsl = LoadXSL();
  22.  
  23.         //Create a stream to write to
  24.         MemoryStream myStreamWriter = new MemoryStream();
  25.         XmlTextWriter mytw = new XmlTextWriter(myStreamWriter, Encoding.UTF8);
  26.         XMLTransform(xsl, xml, mytw);
  27.     }   
  28.  
  29.     private XmlReader LoadXML()
  30.     {
  31.         XmlReaderSettings settings = new XmlReaderSettings();
  32.         settings.ConformanceLevel = ConformanceLevel.Fragment;
  33.         return XmlReader.Create("C:\\...\\input.xml", settings);
  34.     }
  35.  
  36.     private XmlReader LoadXSL()
  37.     {
  38.         XmlReaderSettings settings = new XmlReaderSettings();
  39.         settings.ConformanceLevel = ConformanceLevel.Fragment;
  40.         return XmlReader.Create("C:\\..\\CQCal.xsl", settings);
  41.     }
  42.  
  43.     private void XMLTransform(XmlReader xslt, XmlReader inputxml, XmlTextWriter outputxml)
  44.     {
  45.         TextWriter outputtext = new StreamWriter("C:\\..\\outputxml.txt");
  46.         System.Xml.Xsl.XslCompiledTransform xslTransform = new System.Xml.Xsl.XslCompiledTransform();
  47.         xslTransform.Load(xslt);
  48.  
  49.         xslTransform.Transform(inputxml, outputxml);
  50.  
  51.         //This part checks the output in a file, as I was unsure how to check whether the transform had been performed correctly.
  52.         xslTransform.Transform(inputxml, null, outputtext);
  53.         outputtext.Write(outputtext.ToString());
  54.     }
  55. }
  56.  
Sep 15 '10 #1
1 988
If you hover you mouse over the methods in Visual Studio the intellisense documentation will tell you which exceptions are thrown. You can write code to handle those.

I would imagine you have these possible issues.

  • Cannot open the xml file. File is locked, doesn't exist, etc.
  • Cannot open the xslt file. File is locked, doesn't exist, etc.
  • The xml file is malformed.
  • The xslt file is malformed.
  • There was some error while executing the xslt.

Look at you could and put some try catches in your code when you are loading both of the files and one where you are actually executing the xslt. You can use multiple catch block to determine the exception type.
Sep 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rolf Kemper | last post by:
Dear All, I'm facing the problem below and need some advice/help on it. I'm loading various XML files via the document function into my XSLT. As long all files exist, it's fine. But when a...
39
by: Holly | last post by:
I'm trying to validate my code and I can't figure out what kind of doctype I have. The validator can't tell me anything because it can't move beyond the doctype declaration. ...
3
by: Major_Small | last post by:
would you include infinite loops, possibly infinite loops, unaccounted for user input, infinite recursion, and that type of thing? if so those are probably my biggest problems... more...
13
by: gtux | last post by:
Hi everybody: I'm new in Javascript, I found some code and there is this: var fruit = { 'apple' : { 'weight' : 10, 'cost' : 9}, 'peach' : { 'weight' : 19, 'cost' : 10} }
1
by: Robert | last post by:
Hello, Is there sombody who can tel me what kind of error this is, and how i can fix it. This is the error: ADODB.Field (0x800A0BCD) Either BOF or EOF is True, or the current record has...
5
by: Steven T. Hatton | last post by:
What should I expect the following code to print? Is it defined in the Standard? What does it produce for you? I was kind of surprised by what GCC 4.0.2 made of it. #include <string> #include...
9
by: gold | last post by:
Hello all, I want know abt wht kind of datastructures using both C & C++ internally. Some were said heap, others said tree anyone can explain brief?
0
by: Terance Emory | last post by:
The real reason a dataset is of use in ASP.NET is the XML storage. I never iterate through records using Code anymore, I use XSLT for transforming data so the convenience of the getXML() method...
1
nirmalsingh
by: nirmalsingh | last post by:
Can I generate XSLT using Java Programmatically where Input is a pure xml file. thanks with regards Nirmal
1
by: tvnaidu | last post by:
I have digital video camera connected to PC, how I will know what kind of packets it is?. I can see live using http access to camera. I installed wireshark, I can see TCP and HTTP packets when I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.