序列化命名空间中的根元素,而不是没有命名空间
Serialize root element in a namespace, not without a namespace
我想在使用 XmlSerializer 时获得以下 XML:
<?xml version="1.0"?>
<GetProfileRequest xmlns="urn:veloconnect:profile-1.1">
</GetProfileRequest>
但是当我连载时,我得到以下 XML:
<?xml version="1.0"?>
<GetProfileRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</GetProfileRequest>
序列化代码:
GetProfileRequest request = new GetProfileRequest();
XmlSerializer serialize = new XmlSerializer(typeof(GetProfileRequest));
serialize.Serialize(Response.OutputStream, request);
类:
[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
{
}
是否有可以在 class "GetProfileRequest" 中定义的属性或有助于将 xmlns="urn:veloconnect:profile-1.1" 命名空间添加到 XML 中的属性?
我还尝试通过以下代码手动添加 XmlSerializeNamespace,但这只是删除了根元素中的所有命名空间声明,而不是创建所需的声明。
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "urn:veloconnect:profile-1.1");
// and then call serialize.Serialize with ns
编辑:此解决方案仅对单向有帮助。如果我想用我的 classes 反序列化 XML,那是行不通的。
好吧,我成功尝试了 XmlSerializeNamespace 运行。我比我想象的要近。这不是一个干净的解决方案,因为它是从序列化程序处理的,而不是从 class 本身处理的。但这总比没有解决方案好。如果有人有更好的解决方案,我将不胜感激。
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);
您少了一行代码。
在您的 Class.
中添加以下代码
**[System.Xml.Serialization.XmlRoot("GetProfileRequest", Namespace = "urn:veloconnect:profile-1.1", IsNullable = false)**]
希望它能奏效。它对我有用。
我想在使用 XmlSerializer 时获得以下 XML:
<?xml version="1.0"?>
<GetProfileRequest xmlns="urn:veloconnect:profile-1.1">
</GetProfileRequest>
但是当我连载时,我得到以下 XML:
<?xml version="1.0"?>
<GetProfileRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</GetProfileRequest>
序列化代码:
GetProfileRequest request = new GetProfileRequest();
XmlSerializer serialize = new XmlSerializer(typeof(GetProfileRequest));
serialize.Serialize(Response.OutputStream, request);
类:
[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
{
}
是否有可以在 class "GetProfileRequest" 中定义的属性或有助于将 xmlns="urn:veloconnect:profile-1.1" 命名空间添加到 XML 中的属性?
我还尝试通过以下代码手动添加 XmlSerializeNamespace,但这只是删除了根元素中的所有命名空间声明,而不是创建所需的声明。
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "urn:veloconnect:profile-1.1");
// and then call serialize.Serialize with ns
编辑:此解决方案仅对单向有帮助。如果我想用我的 classes 反序列化 XML,那是行不通的。
好吧,我成功尝试了 XmlSerializeNamespace 运行。我比我想象的要近。这不是一个干净的解决方案,因为它是从序列化程序处理的,而不是从 class 本身处理的。但这总比没有解决方案好。如果有人有更好的解决方案,我将不胜感激。
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);
您少了一行代码。 在您的 Class.
中添加以下代码**[System.Xml.Serialization.XmlRoot("GetProfileRequest", Namespace = "urn:veloconnect:profile-1.1", IsNullable = false)**]
希望它能奏效。它对我有用。