LiquidXML DataBinding导出XML时如何抑制xs:type属性
I’ve run into this exact scenario before with LiquidXML’s DataBinder—those auto-added xs:type attributes can be frustrating when your target XML schema doesn’t require them. Here’s how to fix it, depending on where you are in the workflow:
1. Adjust DataBinder Settings Before Generating Code
If you haven’t already generated your VB.NET classes (or can regenerate them), this is the cleanest approach:
- Open your LiquidXML DataBinder project.
- Navigate to the Serialization Settings (look under project properties or code generation options—labeling may vary slightly).
- Look for an option like "Suppress xsi:type Attributes" or "Omit Type Hints for Derived Classes" and enable it.
- Regenerate your VB.NET classes. The auto-generated
ToXMLmethods will now omit thexs:typeattributes for derived types that the schema explicitly expects.
2. Modify Generated Code Manually
If regenerating code isn’t an option, tweak the generated classes to remove the type hint:
- Locate the class property that’s producing the
xs:typeattribute (e.g., thepatientproperty in yourpairclass). - If the property is declared as a base type (like
Public Property patient As PersonwherePatientinherits fromPerson), update the property’s XML attribute to explicitly specify the derived type:
This tells the serializer that the property will always be a<XmlElement(Type:=GetType(Patient))> Public Property patient As PatientPatientinstance, so it doesn’t need to add thexs:typehint. - Alternatively, check if your base class has an
XmlIncludeattribute forPatient—if you don’t need polymorphism elsewhere, removing this will make the serializer skip the type attribute.
3. Replace the Auto-Generated ToXML Method with Custom Serialization
If the above methods don’t work, roll your own serialization logic to override the auto-generated code:
- Create a custom method in your class instead of using the auto-generated
ToXML:Public Function ToXmlWithoutTypeHints() As String Dim serializer As New XmlSerializer(GetType(Pair)) Dim namespaces As New XmlSerializerNamespaces() ' Remove default namespaces if your schema doesn't require them namespaces.Add("", "") Using writer As New StringWriter() serializer.Serialize(writer, Me, namespaces) Return writer.ToString() End Using End Function - The key here is serializing the concrete type directly (or ensuring the property is typed as
Patientinstead of a base class)—this removes the need for the serializer to add axs:typeattribute to disambiguate the type.
Quick Context
The xs:type attribute is added by default when dealing with polymorphic types (e.g., Patient inheriting from a base class) to let XML parsers know the actual element type. LiquidXML adds this automatically if your XSD defines inheritance. If your target system doesn’t need this hint (or your schema expects the concrete type directly), the steps above will suppress it.
内容的提问来源于stack exchange,提问作者Zero-G.

