?
Solved

XMLTextWriter - Add stylesheet reference to XML File

Posted on 2005-03-15
7
Medium Priority
?
880 Views
Last Modified: 2011-10-03
Hi,

I have written a C# application that retrieves information from an Oracle database and writes this information to an XML file - the format of the XML file is validated against an XSD file (using XMLValidatingReader).

The first line of the XML file created reads:

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

I would like to write an additional line to the XML file immediately after this line that reads:

<?xml-stylesheet type="text/xsl" href="test.xsl"?>

This line includes a reference to the stylesheet 'test.xsl'.

How do I add this line to the XML file created?

Many thanks,

Paul
0
Comment
Question by:Hillsman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 13543407
You use this :

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();

    // Create a procesing instruction.
    XmlProcessingInstruction newPI;
    String PItext = "type='text/xsl' href='book.xsl'";
    newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext);

    // Display the target and data information.
    Console.WriteLine("<?{0} {1}?>", newPI.Target, newPI.Data);

    // Add the processing instruction node to the document.
    doc.AppendChild(newPI);

  }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 13543412
That is an example for the .NET reference for XmlProcessingInstruction and CreateProcessingInstruction();
0
 

Author Comment

by:Hillsman
ID: 13545357
Hi,

Thank you for your reply - I am working on another issue at the moment, but I will try this first thing tomorrow and let you know how I get on.

Cheers,

Paul
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Hillsman
ID: 13553738
Hi,

Admittedly, I'm a complete novice when it comes to C# programming, with XML also being completely new to me. I am creating the XML file using the following code:

>>>>>>>>>>>>>>>
XmlTextWriter writer = new XmlTextWriter("Planning.xml", null);

// Starts a new document
writer.WriteStartDocument();

//Was thinking I could write the additional line to the XML file at this point?!

// Add elements to the XML file

// End the document
writer.WriteEndDocument();
writer.Close();

//Open file stream
FileStream stream = new FileStream("Planning.xml", FileMode.Open);
XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

//Validate XML format against XSD file
vr.Schemas.Add(null, "planning.xsd");
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

while(vr.Read());
vr.Close();
>>>>>>>>>>>>

I imagine that after this point, I need to access the file created and insert the reference the stylesheet Test.xsl. I am unsure how to modify your code to reference the file "Planning.xml" that I have created?

Your help is much appreciated.

Many thanks,

Paul
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 1000 total points
ID: 13553802
You would use :


    String PItext="type='text/xsl' href='test.xsl'";
    writer.WriteProcessingInstruction("xml-stylesheet", PItext);

at the point you have indicated.
0
 

Author Comment

by:Hillsman
ID: 13553868
Hi,

That has done the trick - I figured it would take 1 or 2 lines of code to do it, however all I could find online were blocks of code concerning XML Transformations etc. I suppose the trick with C# is knowing what method to use....which isn't always that easy!

Many thanks for your help and your speedy responses.

All the best,

Paul
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 13553983
OK. My pleasure.

If you have the .NET SDK installed you should have the complete .NET reference will lots of examples.

Try this command :

C:\Program Files\Common Files\Microsoft Shared\Help\dexplore.exe /helpcol ms-help://ms.netframeworksdkv1.1

It may vary slightly depending on installation location and the version of .NET you have.
This should open up a complete .NET reference including language references, class reference, guides etc etc, all fully indexed and searchable.
This is an absolutely invaluable tool. You really do need this or MSDN.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question