序列化时从生成的 xml 中删除 xsi:type
我正在向外部发送 XML。
称为“数据字段”的节点之一具有称为“值”的元素。这可能包含普通文本内容或 html 内容(我需要将其包装在 CData 中)。
因此,我创建了一个基类(ProvisionDataField),其中有 2 个类继承自它(ProvisionTextField 和 ProvisionCDataField),如下所示:
<XmlInclude(GetType(ProvisionTextField))>
<XmlInclude(GetType(ProvisionCDataField))>
Public MustInherit Class ProvisionDataField
<XmlAttribute("datatype")>
Public Property DataType As String
<XmlElement("name")>
Public Property Name As String
End Class
Public Class ProvisionCDataField
Inherits ProvisionDataField
<XmlIgnore()>
Public Property ValueContent As String
<XmlElement("value")>
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
<XmlElement("value")>
Public Property Value As String
End Class
当我序列化时,我得到如下内容:
<entitydata entitytype="company">
<datafield xsi:type="ProvisionTextField" datatype="string">
<name>companyAcronym</name>
<value>testCompany</value>
</datafield>
<datafield xsi:type="ProvisionCDataField" datatype="string">
<name>ssocontent</name>
<value><![CDATA[<html><body> HTML Content</body></html>]]></value>
</datafield>
</entitydata>
一切都很好,除了我被告知必须删除xml 中的“xsi:type”。因此,我需要生成的 xml 看起来像这样:
<entitydata entitytype="company">
<datafield datatype="string">
<name>companyAcronym</name>
<value>testCompany</value>
</datafield>
<datafield datatype="string">
<name>ssocontent</name>
<value><![CDATA[<html><body> HTML Content</body></html>]]></value>
</datafield>
</entitydata>
这可能吗?
I am sending XML externally.
One of the node called "datafield" has an element called "value". This may contain normal text content, or an html content (which I need to wrap in CData).
So, I created a base class (ProvisionDataField) with 2 classes inherits from it (ProvisionTextField, and ProvisionCDataField) as follows:
<XmlInclude(GetType(ProvisionTextField))>
<XmlInclude(GetType(ProvisionCDataField))>
Public MustInherit Class ProvisionDataField
<XmlAttribute("datatype")>
Public Property DataType As String
<XmlElement("name")>
Public Property Name As String
End Class
Public Class ProvisionCDataField
Inherits ProvisionDataField
<XmlIgnore()>
Public Property ValueContent As String
<XmlElement("value")>
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
<XmlElement("value")>
Public Property Value As String
End Class
When I serialize,I get something like this:
<entitydata entitytype="company">
<datafield xsi:type="ProvisionTextField" datatype="string">
<name>companyAcronym</name>
<value>testCompany</value>
</datafield>
<datafield xsi:type="ProvisionCDataField" datatype="string">
<name>ssocontent</name>
<value><![CDATA[<html><body> HTML Content</body></html>]]></value>
</datafield>
</entitydata>
All good except that I've been told that I have to remove the "xsi:type" from the xml. So instead, I need my generated xml to look like this:
<entitydata entitytype="company">
<datafield datatype="string">
<name>companyAcronym</name>
<value>testCompany</value>
</datafield>
<datafield datatype="string">
<name>ssocontent</name>
<value><![CDATA[<html><body> HTML Content</body></html>]]></value>
</datafield>
</entitydata>
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我正在寻找的答案 - 它将确保省略继承中使用的 XmlInclude 属性产生的 xsi:type:
虽然本节将省略根中的 xmlns:xsd 和 xmlns:xsi
完整代码:
This is the answer I am looking for - it will ensure that the xsi:type resulted from XmlInclude attribute used in inheritance is omitted:
While this section will omit the xmlns:xsd and xmlns:xsi from the root
Full codes:
您将必须覆盖 xmlwriter。
这篇博文(不是我的)向您展示了如何操作。
这是 VB.Net 版本。
就这样的结果。
You will have to overwrite xmlwriter.
This blogpost (not mine) shows you how.
Here is the VB.Net version.
With this as the result.
无需重写 XmlWriter,只需使用 XmlSerializerNamespace 的实例:
这将导致 xml 根本没有命名空间。
编辑:更改为 VB 代码
编辑2:经过进一步测试,我使用的测试代码仅从生成的 xml 中删除了名称空间声明。即使我使用了 OP 提供的类,我最初的测试也没有在元素上生成 xsi:type 属性,因此我无法确定我发布的代码是否会删除它们,正如 John Saunder 在评论中提到的那样。我认为如果删除命名空间,那么 xsi:type 属性也会被删除,但我发布的代码并没有证明这一点。
No need to override XmlWriter, just use an instance of XmlSerializerNamespace:
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.