Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following method will output an object to XML without the declaration. The key part is the XmlWriterSettings class. See below. </p> <pre><code>public static string SerializeToString(object obj) { var serializer = new XmlSerializer(obj.GetType()); var ns = new XmlSerializerNamespaces(); ns.Add("", ""); var ms = new MemoryStream(); //the following line omits the xml declaration var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = new UnicodeEncoding(false, false) }; var writer = XmlWriter.Create(ms, settings); serializer.Serialize(writer, obj, ns); return Encoding.Unicode.GetString(ms.ToArray()); } </code></pre> <p>Whereas this method is used when working with class objects, the same principals apply when working with strings. The key parts are the XmlWriterSettings class, and likely (although your post doesn't mention it) the XmlSerializerNamespaces class. </p> <p>The method will return a string with no xml declaration and no namespaces, which works perfectly for the web service fragments I need to use.</p> <p>--Edit--</p> <p>the following short program prints everything without the declaration and without tags: </p> <pre><code>var xml = new XmlDocument(); var fragment = @"&lt;tile&gt; &lt;visual&gt; &lt;binding template='TileWideSmallImageAndText02'&gt; &lt;image id='1' src='http://server/images/{0}_wide.png'/&gt; &lt;text id='1'&gt;Custom Field : {1}/text&gt; &lt;text id='2'&gt;Custom Field : {2}&lt;/text&gt; &lt;text id='3'&gt;Custom Field : {3}&lt;/text&gt; &lt;/binding&gt; &lt;binding template='TileSquarePeekImageAndText01'&gt; &lt;image id='1' src='http://server/images/{0}_square.png'/&gt; &lt;text id='1'&gt;Custom Field&lt;/text&gt; &lt;text id='2'&gt;{1}&lt;/text&gt; &lt;/binding&gt; &lt;/visual&gt; &lt;/tile&gt;"; var returnedXml = SerializeToString(fragment); returnedXml = returnedXml.Replace("&lt;string&gt;", ""); returnedXml = returnedXml.Replace("&lt;/string&gt;", ""); Console.WriteLine(returnedXml); } public static string SerializeToString(string obj) { var serializer = new XmlSerializer(obj.GetType()); var ns = new XmlSerializerNamespaces(); ns.Add("", ""); var ms = new MemoryStream(); //the following line omits the xml declaration var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = new UnicodeEncoding(false, false) }; var writer = XmlWriter.Create(ms, settings); serializer.Serialize(writer, obj, ns); return Encoding.Unicode.GetString(ms.ToArray()); } </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. This table or related slice is empty.
    1. 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