开发者

Serializing BaseClass List without namespace

I have BaseClass List

Public Class Package

  <XmlElement("OBJECT")>
  Public List As List(Of baseobj)
  Public Sub New()
    List = New List(Of baseobj)
    Dim f As New First()
    Dim s As New Second()
    List.Add(s)
    List.Add(f)
  End Sub

  Function Serialize() As String
    Dim XmlSerializer As New XmlSerializer(GetType(Package), "")
    Dim NS As New XmlSerializerNamespaces()
    Dim SB As New StringBuilder
    Dim str As New StringWriter(SB)
    NS.Add("", "")
    XmlSerializer.Serialize(str, Me, NS)
    Return SB.ToString
  End Function
End Class

Base class

<XmlInclude(GetType(First))>
<XmlInclude(GetType(Second))>
Public MustInherit Class baseobj
  Public Common As String
  Public Sub New()
    Common = "_Common"
  End Sub
End Class

And drived classes

Public Class First
  Inherits baseobj
  <XmlAttribute("term")>
  Public FirstAttr As String

  Public Sub New()
    MyBase.New()
    FirstAttr = "FirstAttr"
  End Sub
End Class

Public Class Second
  Inherits baseobj
  <XmlAttribute("term")>
  Public SecAttr As String

  Public Sub New()
    MyBase.New()
    SecAttr = "SecAttr"
  E开发者_运维问答nd Sub
End Class

On serializing package object (pkg.Serialize) have this:

<?xml version="1.0" encoding="utf-16"?>
<Package>
  <OBJECT d2p1:type="Second" term="SecAttr" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
    <Common>_Common</Common>
  </OBJECT>
  <OBJECT d2p1:type="First" term="FirstAttr" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
    <Common>_Common</Common>
  </OBJECT>
</Package>

How can i serialize package object to

<?xml version="1.0" encoding="utf-16"?>
<Package>
  <OBJECT Type="Second" term="SecAttr" >
    <Common>_Common</Common>
  </OBJECT>
  <OBJECT Type="First" term="FirstAttr">
    <Common>_Common</Common>
  </OBJECT>
</Package>

without namespaces and prefixes


You can't do that. Where would the Type attribute come from?

d2p1:type (usually written as xsi:type) is a well-known attribute used to convey the actual type of an element. If you could get rid of the namespace, then it would be meaningless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜