简体   繁体   中英

Serialize root element in a namespace, not without a namespace

I want to get the following XML while using XmlSerializer:

<?xml version="1.0"?>
<GetProfileRequest xmlns="urn:veloconnect:profile-1.1">
</GetProfileRequest>

But when I serialize, I get the following XML:

<?xml version="1.0"?>
<GetProfileRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</GetProfileRequest>

Code for serialization:

GetProfileRequest request = new GetProfileRequest();

XmlSerializer serialize = new XmlSerializer(typeof(GetProfileRequest));
serialize.Serialize(Response.OutputStream, request);

Classes:

[System.Xml.Serialization.XmlIncludeAttribute(typeof(TransactionRequestType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:veloconnect:profile-1.1")]
public partial class GetProfileRequest : RequestType
{
}

[System.Xml.Serialization.XmlIncludeAttribute(typeof(TransactionRequestType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:veloconnect:transaction-1.0")]
public partial class RequestType
{
}

Is there an attribute that can be definied in the class "GetProfileRequest" or something which helps to get the xmlns="urn:veloconnect:profile-1.1" namespace into the XML?

I also tried to add an XmlSerializeNamespace by Hand via following code, but this just removed all namespace declarations in the root element instead of creating the desired declaration.

 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
 ns.Add("", "urn:veloconnect:profile-1.1");
 // and then call serialize.Serialize with ns

You are missing one line code. Add the following code in your Class.

**[System.Xml.Serialization.XmlRoot("GetProfileRequest", Namespace = "urn:veloconnect:profile-1.1", IsNullable = false)**]

Hope it will work. It is working for me.

EDIT: This solution this only help one-way. If I want to deserialize the XML with my classes, it doesn't work.

Well, I was able to get my attempt with the XmlSerializeNamespace running. I was closer than I thought. This is no clean solution, because it's handled from the serializer and not from the class itself. But it's better than no solution. If somebody has a better solution I would be grateful to hear about.

string sDefaultNamespace = "urn:veloconnect:profile-1.1";
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", sDefaultNamespace);

XmlSerializer serialize = new XmlSerializer(ResponseResult.GetType(), sDefaultNamespace);
serialize.Serialize(Response.OutputStream, ResponseResult, ns);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM