Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your first issue is that you don't have XML - you have a stream of bytes which can be loaded into an <code>XDocument</code> or <code>XmlDocument</code>. Do you really need to output to a stream instead of directly into an XML Document?</p> <p>BTW, I think you need to lose the <code>using</code> block around the <code>MemoryStream</code>. You won't be able to use the stream for much after it's been disposed.</p> <pre><code>public void AppendElement() { XDocument doc; using(MemoryStream stream = GetXml()) { using(var sr = new StreamReader(stream)) { doc = XDocument.Load(sr); } } if(doc.Root != null) { doc.Root.Add(new XElement("Whatever")); } } private static MemoryStream GetXml() { var settings = new XmlWriterSettings {Indent = true}; var memoryStream = new MemoryStream(); using (XmlWriter writer = XmlWriter.Create(memoryStream, settings)) { writer.WriteStartDocument(); writer.WriteStartElement("root"); writer.WriteStartElement("element"); writer.WriteString("content"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); } return memoryStream; } </code></pre> <p>For efficiency, try to work directly with <code>XDocument</code>. You seem to want to use <code>XmlWriter</code>, so here's how to do it (not tested):</p> <pre><code>public void AppendElement() { XDocument doc = GetXml(); if(doc.Root != null) { doc.Root.Add(new XElement("Whatever")); } } private static XDocument GetXml() { var doc = new XDocument(); using (XmlWriter writer = doc.CreateWriter()) { writer.WriteStartDocument(); writer.WriteStartElement("root"); writer.WriteStartElement("element"); writer.WriteString("content"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); } return doc; } </code></pre> <hr> <p><strong>Update:</strong> </p> <p>You can write the XML with</p> <pre><code>doc.Save(HttpContext.Response.Output); </code></pre> <hr> <p>Again, I can't test it now, but try this:</p> <pre><code>private static XDocument GetXml() { return new XDocument( new XElement( "root", new XElement( "element", "content"))); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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