Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a Text line in xml on a particular location?
    text
    copied!<p>How can I insert the following stylesheet information into my existing xml file which is created using C#?</p> <pre><code>&lt;?xml-stylesheet type="text/xsl" href="_fileName.xsl"?&gt; </code></pre> <p>Or.... Can I add this line at the time of creation of the new XML file?</p> <p>Edit:</p> <p>I tried to achieve the above using <code>XmlSerialier</code> (hit and trial), something like this:</p> <pre><code>// assumes 'XML' file exists. XmlDocument doc = new XmlDocument(); XElement dataElements = XElement.Load("_fileName.xml"); XmlSerializer xs = new XmlSerializer(typeof(Parents)); var ms = new MemoryStream(); xs.Serialize(ms, parents); ms.Seek(0, SeekOrigin.Begin); // rewind stream to beginning doc.Load(ms); XmlProcessingInstruction pi; string data = "type=\"text/xsl\" href=\"_fileName.xsl\""; pi = doc.CreateProcessingInstruction("xml-stylesheet", data); doc.InsertBefore(pi, doc.DocumentElement); // insert before root doc.DocumentElement.Attributes.RemoveAll(); // remove namespaces </code></pre> <p>But the output xml is getting corrupted:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;?xml-stylesheet type="text/xsl" href="_fileName.xsl"?&gt; &lt;parents /&gt; </code></pre> <p>Whereas the desired output is something like:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;?xml-stylesheet type="text/xsl" href="_fileName.xsl"?&gt; &lt;parents&gt; &lt;parent&gt; &lt;Child1&gt; &lt;child2&gt; &lt;/parent&gt; &lt;/parents&gt; </code></pre> <p>Did this help to understand what's my problem???</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload