Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 908
  • Last Modified:

XMLTextWriter - Add stylesheet reference to XML File

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
Hillsman
Asked:
Hillsman
  • 4
  • 3
1 Solution
 
ozymandiasCommented:
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
 
ozymandiasCommented:
That is an example for the .NET reference for XmlProcessingInstruction and CreateProcessingInstruction();
0
 
HillsmanAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
HillsmanAuthor Commented:
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
 
ozymandiasCommented:
You would use :


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

at the point you have indicated.
0
 
HillsmanAuthor Commented:
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
 
ozymandiasCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now