Dynamically Creating XML Documents Error

A

accyboy1981

Hi,

I've create a web applicatation the create an xml document dynamically.
Now that is does this I would like to add a stylesheet to the xml
document. I'm using the following code:

XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8",
null);

doc.PrependChild(declaration);

XmlProcessingInstruction objStyleSheet =
doc.CreateProcessingInstruction("xml-stylesheet","");

XmlAttribute objAttType = doc.CreateAttribute("type");
objAttType.Value = "text/css";

XmlAttribute objAttHref = doc.CreateAttribute("href");
objAttHref.Value = "xml.css";

objStyleSheet.Attributes.Append(objAttType);
objStyleSheet.Attributes.Append(objAttHref);

It compiles find however when running it shows the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

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

Source Error:


Line 153: objAttHref.Value = "xml.css";
Line 154:
Line 155: objStyleSheet.Attributes.Append(objAttType);
Line 156: objStyleSheet.Attributes.Append(objAttHref);

Any help would be much appreciated.

Thanks

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top