Remove xsi:type from generated xml when serializing?

You will have to overwrite xmlwriter. This blogpost (not mine) shows you how. Here is the VB.Net version.

Imports System.Xml. Serialization Imports System. Xml Imports System.

IO Imports System. Text Module Module1 Sub Main() Dim p As New ProvisionCDataField() p.Name = "test" Dim sw1 = New StringWriter() Dim xs1 As New XmlSerializer(GetType(ProvisionDataField)) xs1. Serialize(New XmlTextWriter(sw1), p) Console.

WriteLine(sw1.ToString()) Dim sw2 = New StringWriter() Dim xs2 As New XmlSerializer(GetType(ProvisionDataField)) xs2. Serialize(New NonXsiTextWriter(sw2), p) Console. WriteLine(sw2.ToString()) Console.ReadLine() End Sub End Module Public Class NonXsiTextWriter Inherits XmlTextWriter Public Sub New(ByVal w As TextWriter) MyBase.

New(w) End Sub Public Sub New(ByVal w As Stream, ByVal encoding As Encoding) MyBase. New(w, encoding) End Sub Public Sub New(ByVal filename As String, ByVal encoding As Encoding) MyBase. New(filename, encoding) End Sub Dim _skip As Boolean = False Public Overrides Sub WriteStartAttribute(ByVal prefix As String, ByVal localName As String, ByVal ns As String) If localName = "xsi" Then _skip = True Return End If MyBase.

WriteStartAttribute(prefix, localName, ns) End Sub Public Overrides Sub WriteString(ByVal text As String) If _skip Then Return MyBase. WriteString(text) End Sub Public Overrides Sub WriteEndAttribute() If _skip Then _skip = False Return End If MyBase. WriteEndAttribute() End Sub End Class Public MustInherit Class ProvisionDataField Public Property DataType As String Public Property Name As String End Class Public Class ProvisionCDataField Inherits ProvisionDataField Public Property ValueContent As String Public Property Value() As XmlCDataSection Get Dim doc As New XmlDocument Return doc.

CreateCDataSection(ValueContent) End Get Set(ByVal value As XmlCDataSection) ValueContent = value. Value End Set End Property End Class Public Class ProvisionTextField Inherits ProvisionDataField Public Property Value As String End Class With this as the result. Test test.

You result still has the namespaces which the OP wanted to get rid of – Chris Dunaway Oct 5 at 14:03 -1 for showing an example of how to use new XmlTextWriter(), which has been deprecated since . NET 2.0. – John Saunders Oct 5 at 15:36 @John it has not been deprecated but xmlwriter.

Create is now the prevered way of doing things msdn.microsoft. Com/en-us/library/system.xml.xmltextwriter. Aspx – chrissie1 Oct 5 at 17:47 @chrissie1: that's a pretty good definition of "deprecated".

– John Saunders Oct 5 at 17:56 2 That's a pretty strong claim John. I believe ObsoleteAttribute was introduced specifically to mark things as deprecated (not to mention the deprecated member list msdn.microsoft. Com/en-us/library/ee471421.

Aspx). Less preferred! = deprecated.

– Tarwn Oct 5 at 18:18.

This is the answer I am looking for - it will ensure that the xsi:type resulted from XmlInclude attribute used in inheritance is omitted: ElseIf ns = XmlSchema. InstanceNamespace Then ' Omits all XSI attributes _skip = True Return End If While this section will omit the xmlns:xsd and xmlns:xsi from the root If prefix = "xmlns" AndAlso (localName = "xsd" OrElse localName = "xsi") Then ' Omits XSD and XSI from root _skip = True Return Full codes: Imports System. IO Imports System.

Xml Imports System.Xml. Schema Public Class PlainXmlTextWriter Inherits XmlTextWriter Public Sub New(ByVal w As TextWriter) MyBase. New(w) End Sub Public Sub New(ByVal w As Stream, ByVal encoding As Encoding) MyBase.

New(w, encoding) End Sub Public Sub New(ByVal filename As String, ByVal encoding As Encoding) MyBase. New(filename, encoding) End Sub Dim _skip As Boolean = False Public Overrides Sub WriteStartAttribute(ByVal prefix As String, ByVal localName As String, ByVal ns As String) If prefix = "xmlns" AndAlso (localName = "xsd" OrElse localName = "xsi") Then ' Omits XSD and XSI from root _skip = True Return ElseIf ns = XmlSchema. InstanceNamespace Then ' Omits all XSI attributes _skip = True Return End If MyBase.

WriteStartAttribute(prefix, localName, ns) End Sub Public Overrides Sub WriteString(ByVal text As String) If _skip Then Return MyBase. WriteString(text) End Sub Public Overrides Sub WriteEndAttribute() If _skip Then _skip = False Return End If MyBase. WriteEndAttribute() End Sub End Class.

No need to override XmlWriter, just use an instance of XmlSerializerNamespace: Sub Main() Dim xSer As New XmlSerializer(GetType(MyType)) Dim sb As New StringBuilder() Dim obj As MyType = getAnInstanceOfMyType() Using wrt As New StringWriter(sb) Dim ns As New XmlSerializerNamespaces ns. Add("", "") xSer. Serialize(wrt, obj, ns) End Using Console.

WriteLine(sb.ToString()) Console.ReadLine() End Sub This will result in the xml having no namespaces at all. EDIT: Changed to VB code EDIT 2: Upon further testing, the test code I used only removed the namespace declarations from the resulting xml. My original test did not produce the xsi:type attributes on the elements even though I used the classes provided by the OP, so I cannot determine if the code that I posted will remove them, as John Saunder alluded to in the comments.

I presumed that if the namespaces were removed, then the xsi:type attributes would also be removed, but the code I posted does not prove this.

1 doesn't help with xsi:type. – John Saunders Oct 5 at 15:34 I tested the code before posting and it seemed to work, the xsi type was removed. – Chris Dunaway Oct 5 at 16:15.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions