Stefan Steiger
Stefan Steiger

Reputation: 82196

How to PROPERLY remove xmln:xsi and xmlns:xsd from xml dictionary serialization

Question: I use a serializable dictionary class, found at
http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
, to serialize a dictionary.
It works fine with the example class below.

<System.Xml.Serialization.XmlRoot("ccl")> _
Public Class ccl
    <System.Xml.Serialization.XmlElement("name")> _
    Public xx As String = ""

    <System.Xml.Serialization.XmlElement("date")> _
    Public yy As String = ""


    '<System.Xml.Serialization.XmlElement("adict")> _
    'Public ww As New SerializableDictionary(Of String, String)

End Class

But it adds a,

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance AND xmlns:xsd="http://www.w3.org/2001/XMLSchema?

to the tag

Now I fixed it by changing the dictionary class to

       Dim ns As System.Xml.Serialization.XmlSerializerNamespaces = New System.Xml.Serialization.XmlSerializerNamespaces()
        'Add an empty namespace and empty value
        ns.Add("", "")

        If True Then
            valueSerializer.Serialize(writer, value, ns)
        Else
            valueSerializer.Serialize(writer, value)
        End If

which removes those attributes.

But that also means it does not write them if I specify them. How can I add those two empty namespaces to the class using attributes ?

I changed it to

<System.Xml.Serialization.XmlRoot("ccl", Namespace:="")>

but that doesn't seem to work.

Upvotes: 1

Views: 5182

Answers (1)

KenL
KenL

Reputation: 875

Here is the code I use to serialize to my object o

XmlSerializerNamespaces XSN = new XmlSerializerNamespaces();
XSN.Add("", "");
XmlWriterSettings XWS = new XmlWriterSettings();
XWS.OmitXmlDeclaration = true;
StringBuilder XmlStr = new StringBuilder();
XmlSerializer x = new XmlSerializer(o.GetType());
x.Serialize(XmlTextWriter.Create(XmlStr, XWS), o, XSN);

Upvotes: 2

Related Questions