Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Im generating an XMl document from a stored procedure. After its created i want to insert the below element to point to a xsl file. How do i do this in c# and/or stored procedure

<xml-stylesheet type ='text/xsl' href= 'x.xsl'?>


should look something like this when inserted

<br />
<?xml-stylesheet type ='text/xsl' href= 'x.xsl'?><br />
<catalog><br />
    <cd><br />
        <title>Empire Burlesque</title><br />
        <artist>Bob Dylan</artist><br />
......<br />
Posted

C#
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");



doc.PrependChild(doc.CreateProcessingInstruction(
     "xml-stylesheet",
     "type='text/xsl' href='book.xsl'"));




// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter("data.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);


See:http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createprocessinginstruction.asp[^]
Just cleaned it up a bit... :-D
 
Share this answer
 
v4
Comments
ONeil Tomlinson 21-Sep-10 9:41am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
SQL
Declare @Stylesheet as XML
Declare @xOutput as XML
DECLARE @XmlTable TABLE (	
	Element0 int,
	Element1 nvarchar(100),
	Element2 nvarchar(200))
	
INSERT INTO @XmlTable ([Element0] ,[Element1],[Element2])
     VALUES (1,'bobo','Monkey')		

-- Convert Table data into XML
SET @xOutput = (Select *From @XmlTable
	FOR XML PATH( 'DataItem' ), ROOT('Root'))
select @xOutput

select @xOutput.query('
<?xml-stylesheet type="text/xsl" href="my-style-sheet.xsl" ?>,/.')


See also:http://blogs.msdn.com/b/mrorke/archive/2005/05/10/416033.asp[^]
 
Share this answer
 
v2
Comments
ONeil Tomlinson 17-Sep-10 10:54am    
Oh thats pretty cool. Thanks
I should have ask how to do this in c# and not in a stored procedure as the "href" value will be taken from a config file.
createXML.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='gss.xsl'");
 
Share this answer
 
Comments
Matt T Heffron 29-Jan-16 21:47pm    
This question was asked and answered over 4 years ago!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900