Note that there are some explanatory texts on larger screens.

plurals
  1. POXmlSerializer and controlling namespace in XmlAnyElement
    text
    copied!<p>using dotnet 2.0. Code to illustrate :</p> <pre><code> Class1 c1 = new Class1(); c1.SomeInt = 5; XmlDocument doc = new XmlDocument(); doc.LoadXml("&lt;anode xmlns=\"xyz\" &gt;&lt;id&gt;123&lt;/id&gt;&lt;/anode&gt;"); c1.Any = new XmlElement[1]; c1.Any[0] = (XmlElement)doc.DocumentElement; XmlSerializer ser = new XmlSerializer(typeof(Class1)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "xyz"); StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; XmlWriter writer = XmlWriter.Create(sb, settings); writer.WriteStartElement("root"); ser.Serialize(writer, c1, ns); writer.WriteEndElement(); writer.Close(); string str = sb.ToString(); MessageBox.Show(str); </code></pre> <p>where Class1 is defined like :</p> <pre><code>[System.Serializable()] [System.Xml.Serialization.XmlRoot(Namespace="xyz")] public class Class1 { private int someInt; public int SomeInt { get { return someInt; } set { someInt = value; } } private System.Xml.XmlElement[] anyField; /// &lt;remarks/&gt; [System.Xml.Serialization.XmlAnyElementAttribute()] public System.Xml.XmlElement[] Any { get { return this.anyField; } set { this.anyField = value; } } } </code></pre> <p>This code displays the string : </p> <pre><code>&lt;root&gt;&lt;Class1 xmlns="xyz"&gt;&lt;SomeInt&gt;5&lt;/SomeInt&gt;&lt;anode xmlns="xyz"&gt;&lt;id&gt;123&lt;/id&gt;&lt;/anode&gt;&lt;/Class1&gt;&lt;/root&gt; </code></pre> <p>This is the correct xml, but I'm wondering if this can be simplified. </p> <p>What I would like is to not have the redundant xmlns="xyz" part in the "anode" element. i.e. I would like : </p> <pre><code>&lt;root&gt;&lt;Class1 xmlns="xyz"&gt;&lt;SomeInt&gt;5&lt;/SomeInt&gt;&lt;anode&gt;&lt;id&gt;123&lt;/id&gt;&lt;/anode&gt;&lt;/Class1&gt;&lt;/root&gt; </code></pre> <p>Is this possible ?</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