XmlSerializer c++ 使用多个命名空间反序列化

Posted

技术标签:

【中文标题】XmlSerializer c++ 使用多个命名空间反序列化【英文标题】:XmlSerializer c++ Deserialize with multiple namespaces 【发布时间】:2015-06-30 06:53:12 【问题描述】:

我遇到以下问题,涉及 Microsoft (c++) 的 XmlSerializer 和反序列化过程。 ds:signature 没有反序列化,我得到了 Undentified 值。有人能发现任何错误吗?我的代码如下: 我的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<test  xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <aa>
        <version>0.1.1</version>
    </aa>
    <bb>
        <cc>
            <Id>134324321421</Id>
            <ds:Signature>
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/>
                    <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
                    <ds:Reference Id="1" Type="" URI="something">
                        <ds:Transforms>
                            <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                        <ds:DigestValue>43=</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>
                4344
            </ds:SignatureValue>
                <ds:KeyInfo>
                    <ds:KeyValue>
                        <ds:RSAKeyValue>
                            <ds:Modulus>
                            4543434
                        </ds:Modulus>
                            <ds:Exponent>43</ds:Exponent>
                        </ds:RSAKeyValue>
                    </ds:KeyValue>
                    <ds:X509Data>
                        <ds:X509Certificate>
                        3454343243242
                    </ds:X509Certificate>
                    </ds:X509Data>
                </ds:KeyInfo>
            </ds:Signature>
        </cc>
    </bb>
</aa>

而我的代码如下:

#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 
using namespace System::Xml::Serialization;
using namespace System;
namespace ds

ref class _KeyInfo;
ref class _KeyValue;
ref class _RSAKeyValue;
ref class _X509Data;
ref class _Reference;
ref class _Transforms;
ref class _Transform;
ref class _DigestMethod;
ref class _Signature;
ref class _SignedInfo;
ref class _CanonicalizationMethod;
ref class _SignatureMethod;


/// <remarks/>

public ref class _KeyInfo 

    private: _KeyValue^  keyValueField;

    private: _X509Data^  x509DataField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("KeyValue", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _KeyValue^  KeyValue 
        _KeyValue^  get();
        System::Void set(_KeyValue^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("X509Data", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _X509Data^  X509Data 
        _X509Data^  get();
        System::Void set(_X509Data^  value);
    
;


public ref class _KeyValue 

    private: _RSAKeyValue^  rSAKeyValueField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("RSAKeyValue", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _RSAKeyValue^  RSAKeyValue 
        _RSAKeyValue^  get();
        System::Void set(_RSAKeyValue^  value);
    
;


public ref class _RSAKeyValue 

    private: System::String^  modulusField;

    private: System::String^ exponentField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Modulus", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  Modulus 
        System::String^  get();
        System::Void set(System::String^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Exponent", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^ Exponent 
        System::String^ get();
        System::Void set(System::String^ value);
    
;


public ref class _X509Data 

    private: System::String^  x509CertificateField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("X509Certificate", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  X509Certificate 
        System::String^  get();
        System::Void set(System::String^  value);
    
;


public ref class _Reference 

    private: _Transforms^  transformsField;

    private: _DigestMethod^  digestMethodField;

    private: System::String^ digestValueField;

    private: System::String^ idField;

    private: System::String^ typeField;

    private: System::String^ uRIField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Transforms", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _Transforms^  Transforms 
        _Transforms^  get();
        System::Void set(_Transforms^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("DigestMethod", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _DigestMethod^  DigestMethod 
        _DigestMethod^  get();
        System::Void set(_DigestMethod^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("DigestValue", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^ DigestValue 
        System::String^ get();
        System::Void set(System::String^ value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Id", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^ Id 
        System::String^ get();
        System::Void set(System::String^ value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Type", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^ Type 
        System::String^ get();
        System::Void set(System::String^ value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("URI", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^ URI 
        System::String^ get();
        System::Void set(System::String^ value);
    
;


public ref class _Transforms 

    private: _Transform^  transformField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Transform", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _Transform^  Transform 
        _Transform^  get();
        System::Void set(_Transform^  value);
    
;


public ref class _Transform 

    private: System::String^  algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Algorithm", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;

[System::Xml::Serialization::XmlRoot("DigestMethod", Namespace = "http://www.w3.org/2000/09/xmldsig")]
public ref class _DigestMethod 

private: System::String ^algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Algorithm", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;



public ref class _SignedInfo 

    private: _CanonicalizationMethod^  canonicalizationMethodField;

    private: _SignatureMethod^  signatureMethodField;

    private: _Reference^  referenceField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("CanonicalizationMethod", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _CanonicalizationMethod^  CanonicalizationMethod 
        _CanonicalizationMethod^  get();
        System::Void set(_CanonicalizationMethod^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("SignatureMethod", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _SignatureMethod^  SignatureMethod 
        _SignatureMethod^  get();
        System::Void set(_SignatureMethod^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Reference", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _Reference^  Reference 
        _Reference^  get();
        System::Void set(_Reference^  value);
    
;


public ref class _CanonicalizationMethod 

    private: System::String^  algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Algorithm", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;


public ref class _SignatureMethod 

    private: System::String ^algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Algorithm", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;



public ref class signature 

    private: _SignedInfo^  signedInfoField;

    private: System::String^  signatureValueField;

    private: _KeyInfo^  keyInfoField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("SignedInfo", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _SignedInfo^  SignedInfo 
        _SignedInfo^  get();
        System::Void set(_SignedInfo^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("SignatureValue", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^  SignatureValue 
        System::String^  get();
        System::Void set(System::String^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("KeyInfo", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property _KeyInfo^  KeyInfo 
        _KeyInfo^  get();
        System::Void set(_KeyInfo^  value);
    
;




inline _KeyValue^  _KeyInfo::KeyValue::get() 
    return this->keyValueField;

inline System::Void _KeyInfo::KeyValue::set(_KeyValue^  value) 
    this->keyValueField = value;


inline _X509Data^  _KeyInfo::X509Data::get() 
    return this->x509DataField;

inline System::Void _KeyInfo::X509Data::set(_X509Data^  value) 
    this->x509DataField = value;



inline _RSAKeyValue^  _KeyValue::RSAKeyValue::get() 
    return this->rSAKeyValueField;

inline System::Void _KeyValue::RSAKeyValue::set(_RSAKeyValue^  value) 
    this->rSAKeyValueField = value;



inline System::String^  _RSAKeyValue::Modulus::get() 
    return this->modulusField;

inline System::Void _RSAKeyValue::Modulus::set(System::String^  value) 
    this->modulusField = value;


inline System::String^ _RSAKeyValue::Exponent::get() 
    return this->exponentField;

inline System::Void _RSAKeyValue::Exponent::set(System::String^ value) 
    this->exponentField = value;



inline System::String^  _X509Data::X509Certificate::get() 
    return this->x509CertificateField;

inline System::Void _X509Data::X509Certificate::set(System::String^  value) 
    this->x509CertificateField = value;



inline _Transforms^  _Reference::Transforms::get() 
    return this->transformsField;

inline System::Void _Reference::Transforms::set(_Transforms^  value) 
    this->transformsField = value;


inline _DigestMethod^  _Reference::DigestMethod::get() 
    return this->digestMethodField;

inline System::Void _Reference::DigestMethod::set(_DigestMethod^  value) 
    this->digestMethodField = value;


inline System::String^ _Reference::DigestValue::get() 
    return this->digestValueField;

inline System::Void _Reference::DigestValue::set(System::String^ value) 
    this->digestValueField = value;


inline System::String^ _Reference::Id::get() 
    return this->idField;

inline System::Void _Reference::Id::set(System::String^ value) 
    this->idField = value;


inline System::String^ _Reference::Type::get() 
    return this->typeField;

inline System::Void _Reference::Type::set(System::String^ value) 
    this->typeField = value;


inline System::String^ _Reference::URI::get() 
    return this->uRIField;

inline System::Void _Reference::URI::set(System::String^ value) 
    this->uRIField = value;



inline _Transform^  _Transforms::Transform::get() 
    return this->transformField;

inline System::Void _Transforms::Transform::set(_Transform^  value) 
    this->transformField = value;



inline System::String^  _Transform::Algorithm::get() 
    return this->algorithmField;

inline System::Void _Transform::Algorithm::set(System::String^  value) 
    this->algorithmField = value;



inline System::String^ _DigestMethod::Algorithm::get() 
    return this->algorithmField;

inline System::Void _DigestMethod::Algorithm::set(System::String^ value) 
    this->algorithmField = value;



inline _SignedInfo^  signature::SignedInfo::get() 
    return this->signedInfoField;

inline System::Void signature::SignedInfo::set(_SignedInfo^  value) 
    this->signedInfoField = value;


inline System::String^  signature::SignatureValue::get() 
    return this->signatureValueField;

inline System::Void signature::SignatureValue::set(System::String^  value) 
    this->signatureValueField = value;


inline _KeyInfo^  signature::KeyInfo::get() 
    return this->keyInfoField;

inline System::Void signature::KeyInfo::set(_KeyInfo^  value) 
    this->keyInfoField = value;



inline _CanonicalizationMethod^  _SignedInfo::CanonicalizationMethod::get() 
    return this->canonicalizationMethodField;

inline System::Void _SignedInfo::CanonicalizationMethod::set(_CanonicalizationMethod^  value) 
    this->canonicalizationMethodField = value;


inline _SignatureMethod^  _SignedInfo::SignatureMethod::get() 
    return this->signatureMethodField;

inline System::Void _SignedInfo::SignatureMethod::set(_SignatureMethod^  value) 
    this->signatureMethodField = value;


inline _Reference^  _SignedInfo::Reference::get() 
    return this->referenceField;

inline System::Void _SignedInfo::Reference::set(_Reference^  value) 
    this->referenceField = value;



inline System::String^  _CanonicalizationMethod::Algorithm::get() 
    return this->algorithmField;

inline System::Void _CanonicalizationMethod::Algorithm::set(System::String^  value) 
    this->algorithmField = value;



inline System::String^ _SignatureMethod::Algorithm::get() 
    return this->algorithmField;

inline System::Void _SignatureMethod::Algorithm::set(System::String^ value) 
    this->algorithmField = value;

;

///====================================================================================

public ref class AA
public:
    System::String^ version;
;

public ref class CC
public:
    System::String^ Id;

    public: [System::Xml::Serialization::XmlElement("Signature", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    ds::signature^  Signature 
;

public ref class BB
public:
    CC ^cc
;

[System::Xml::Serialization::XmlRoot("test", Namespace = "")]
public ref class test 
public:
    AA ^aa;
    BB ^bb;

;

//==============================================================================

           NameTable^ nt = gcnew NameTable();
           XmlNamespaceManager ^ namesp = gcnew XmlNamespaceManager (nt);

           namesp->AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
           XmlSerializer^ serializer = gcnew XmlSerializer( test::typeid, "http://www.w3.org/2000/09/xmldsig#");

           // A FileStream is needed to read the XML document.
           FileStream^ fs = gcnew FileStream( filename,FileMode::Open );

           XmlReader^ reader = gcnew XmlTextReader( fs,namesp->NameTable);

           // Declare an object variable of the type to be deserialized.
           test^ i;

           // Use the Deserialize method to restore the object's state.
           i = dynamic_cast<test^>(serializer->Deserialize( reader));

【问题讨论】:

【参考方案1】:

这里有几个问题:

    XML 中的命名空间是 http://www.w3.org/2000/09/xmldsig#,但在您的 c++ 属性装饰中,您省略了结尾的 #,例如:

    public: [System::Xml::Serialization::XmlElement("Signature", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    ds::signature^  Signature 
    

    命名空间不匹配导致无法读取签名。

    您用XmlElement 修饰的几个字段实际上在XML 文件中显示为属性,因此需要用XmlAttribute 修饰,例如:

    public: [System::Xml::Serialization::XmlElement("Id", Namespace = "http://www.w3.org/2000/09/xmldsig")]
    property System::String^ Id 
        System::String^ get();
        System::Void set(System::String^ value);
    
    

    此外,所有需要标记为属性的字段也需要放在空命名空间中。例如,在&lt;ds:Reference Id="1" Type="" URI="something"&gt; 行中,注意到IdTypeURI 确实ds: 前缀?这意味着它们不在任何命名空间中。

在下面的代码中修复了这些问题后,我可以阅读您提供的 XML。

public ref class _KeyInfo 

    private: _KeyValue^  keyValueField;

    private: _X509Data^  x509DataField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("KeyValue", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _KeyValue^  KeyValue 
        _KeyValue^  get();
        System::Void set(_KeyValue^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("X509Data", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _X509Data^  X509Data 
        _X509Data^  get();
        System::Void set(_X509Data^  value);
    
;


public ref class _KeyValue 

    private: _RSAKeyValue^  rSAKeyValueField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("RSAKeyValue", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _RSAKeyValue^  RSAKeyValue 
        _RSAKeyValue^  get();
        System::Void set(_RSAKeyValue^  value);
    
;


public ref class _RSAKeyValue 

    private: System::String^  modulusField;

    private: System::String^ exponentField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Modulus", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property System::String^  Modulus 
        System::String^  get();
        System::Void set(System::String^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Exponent", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property System::String^ Exponent 
        System::String^ get();
        System::Void set(System::String^ value);
    
;


public ref class _X509Data 

    private: System::String^  x509CertificateField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("X509Certificate", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property System::String^  X509Certificate 
        System::String^  get();
        System::Void set(System::String^  value);
    
;


public ref class _Reference 

    private: _Transforms^  transformsField;

    private: _DigestMethod^  digestMethodField;

    private: System::String^ digestValueField;

    private: System::String^ idField;

    private: System::String^ typeField;

    private: System::String^ uRIField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Transforms", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _Transforms^  Transforms 
        _Transforms^  get();
        System::Void set(_Transforms^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("DigestMethod", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _DigestMethod^  DigestMethod 
        _DigestMethod^  get();
        System::Void set(_DigestMethod^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("DigestValue", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property System::String^ DigestValue 
        System::String^ get();
        System::Void set(System::String^ value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("Id", Namespace = "")]
    property System::String^ Id 
        System::String^ get();
        System::Void set(System::String^ value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("Type", Namespace = "")]
    property System::String^ Type 
        System::String^ get();
        System::Void set(System::String^ value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("URI", Namespace = "")]
    property System::String^ URI 
        System::String^ get();
        System::Void set(System::String^ value);
    
;


public ref class _Transforms 

    private: _Transform^  transformField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Transform", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _Transform^  Transform 
        _Transform^  get();
        System::Void set(_Transform^  value);
    
;


public ref class _Transform 

    private: System::String^  algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("Algorithm", Namespace = "")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;

[System::Xml::Serialization::XmlRoot("DigestMethod", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
public ref class _DigestMethod 

private: System::String ^algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("Algorithm", Namespace = "")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;



public ref class _SignedInfo 

    private: _CanonicalizationMethod^  canonicalizationMethodField;

    private: _SignatureMethod^  signatureMethodField;

    private: _Reference^  referenceField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("CanonicalizationMethod", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _CanonicalizationMethod^  CanonicalizationMethod 
        _CanonicalizationMethod^  get();
        System::Void set(_CanonicalizationMethod^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("SignatureMethod", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _SignatureMethod^  SignatureMethod 
        _SignatureMethod^  get();
        System::Void set(_SignatureMethod^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("Reference", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _Reference^  Reference 
        _Reference^  get();
        System::Void set(_Reference^  value);
    
;


public ref class _CanonicalizationMethod 

    private: System::String^  algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("Algorithm", Namespace = "")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;


public ref class _SignatureMethod 

    private: System::String ^algorithmField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlAttribute("Algorithm", Namespace = "")]
    property System::String^  Algorithm 
        System::String^  get();
        System::Void set(System::String^  value);
    
;



public ref class signature 

    private: _SignedInfo^  signedInfoField;

    private: System::String^  signatureValueField;

    private: _KeyInfo^  keyInfoField;

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("SignedInfo", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _SignedInfo^  SignedInfo 
        _SignedInfo^  get();
        System::Void set(_SignedInfo^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("SignatureValue", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property System::String^  SignatureValue 
        System::String^  get();
        System::Void set(System::String^  value);
    

    /// <remarks/>
    public: [System::Xml::Serialization::XmlElement("KeyInfo", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    property _KeyInfo^  KeyInfo 
        _KeyInfo^  get();
        System::Void set(_KeyInfo^  value);
    
;




inline _KeyValue^  _KeyInfo::KeyValue::get() 
    return this->keyValueField;

inline System::Void _KeyInfo::KeyValue::set(_KeyValue^  value) 
    this->keyValueField = value;


inline _X509Data^  _KeyInfo::X509Data::get() 
    return this->x509DataField;

inline System::Void _KeyInfo::X509Data::set(_X509Data^  value) 
    this->x509DataField = value;



inline _RSAKeyValue^  _KeyValue::RSAKeyValue::get() 
    return this->rSAKeyValueField;

inline System::Void _KeyValue::RSAKeyValue::set(_RSAKeyValue^  value) 
    this->rSAKeyValueField = value;



inline System::String^  _RSAKeyValue::Modulus::get() 
    return this->modulusField;

inline System::Void _RSAKeyValue::Modulus::set(System::String^  value) 
    this->modulusField = value;


inline System::String^ _RSAKeyValue::Exponent::get() 
    return this->exponentField;

inline System::Void _RSAKeyValue::Exponent::set(System::String^ value) 
    this->exponentField = value;



inline System::String^  _X509Data::X509Certificate::get() 
    return this->x509CertificateField;

inline System::Void _X509Data::X509Certificate::set(System::String^  value) 
    this->x509CertificateField = value;



inline _Transforms^  _Reference::Transforms::get() 
    return this->transformsField;

inline System::Void _Reference::Transforms::set(_Transforms^  value) 
    this->transformsField = value;


inline _DigestMethod^  _Reference::DigestMethod::get() 
    return this->digestMethodField;

inline System::Void _Reference::DigestMethod::set(_DigestMethod^  value) 
    this->digestMethodField = value;


inline System::String^ _Reference::DigestValue::get() 
    return this->digestValueField;

inline System::Void _Reference::DigestValue::set(System::String^ value) 
    this->digestValueField = value;


inline System::String^ _Reference::Id::get() 
    return this->idField;

inline System::Void _Reference::Id::set(System::String^ value) 
    this->idField = value;


inline System::String^ _Reference::Type::get() 
    return this->typeField;

inline System::Void _Reference::Type::set(System::String^ value) 
    this->typeField = value;


inline System::String^ _Reference::URI::get() 
    return this->uRIField;

inline System::Void _Reference::URI::set(System::String^ value) 
    this->uRIField = value;



inline _Transform^  _Transforms::Transform::get() 
    return this->transformField;

inline System::Void _Transforms::Transform::set(_Transform^  value) 
    this->transformField = value;



inline System::String^  _Transform::Algorithm::get() 
    return this->algorithmField;

inline System::Void _Transform::Algorithm::set(System::String^  value) 
    this->algorithmField = value;



inline System::String^ _DigestMethod::Algorithm::get() 
    return this->algorithmField;

inline System::Void _DigestMethod::Algorithm::set(System::String^ value) 
    this->algorithmField = value;



inline _SignedInfo^  signature::SignedInfo::get() 
    return this->signedInfoField;

inline System::Void signature::SignedInfo::set(_SignedInfo^  value) 
    this->signedInfoField = value;


inline System::String^  signature::SignatureValue::get() 
    return this->signatureValueField;

inline System::Void signature::SignatureValue::set(System::String^  value) 
    this->signatureValueField = value;


inline _KeyInfo^  signature::KeyInfo::get() 
    return this->keyInfoField;

inline System::Void signature::KeyInfo::set(_KeyInfo^  value) 
    this->keyInfoField = value;



inline _CanonicalizationMethod^  _SignedInfo::CanonicalizationMethod::get() 
    return this->canonicalizationMethodField;

inline System::Void _SignedInfo::CanonicalizationMethod::set(_CanonicalizationMethod^  value) 
    this->canonicalizationMethodField = value;


inline _SignatureMethod^  _SignedInfo::SignatureMethod::get() 
    return this->signatureMethodField;

inline System::Void _SignedInfo::SignatureMethod::set(_SignatureMethod^  value) 
    this->signatureMethodField = value;


inline _Reference^  _SignedInfo::Reference::get() 
    return this->referenceField;

inline System::Void _SignedInfo::Reference::set(_Reference^  value) 
    this->referenceField = value;



inline System::String^  _CanonicalizationMethod::Algorithm::get() 
    return this->algorithmField;

inline System::Void _CanonicalizationMethod::Algorithm::set(System::String^  value) 
    this->algorithmField = value;



inline System::String^ _SignatureMethod::Algorithm::get() 
    return this->algorithmField;

inline System::Void _SignatureMethod::Algorithm::set(System::String^ value) 
    this->algorithmField = value;

;

///====================================================================================

public ref class AA
public:
    System::String^ version;
;

public ref class CC
public:
    System::String^ Id;

    public: [System::Xml::Serialization::XmlElement("Signature", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
    ds::signature^  Signature; 
;

public ref class BB
public:
    CC ^cc;
;

[System::Xml::Serialization::XmlRoot("test", Namespace = "")]
public ref class test 
public:
    AA ^aa;
    BB ^bb;

;

将来,检查您的工作的一种方法是使用xsd.exe to generate c# classes for your XML,然后将自动生成的 c# 类与您手动创建的 c++ 类进行比较。你可能已经看到了这个问题。

【讨论】:

以上是关于XmlSerializer c++ 使用多个命名空间反序列化的主要内容,如果未能解决你的问题,请参考以下文章

使用 XmlSerializer 重命名平面 xml 数组中的数组项

c# XmlSerializer 反序列化器缺少默认命名空间

XmlSerializer和可空属性

我可以让 XmlSerializer 在反序列化时忽略命名空间吗?

C# XmlSerializer 用不同的命名空间序列化同一个类

XmlSerializer 空引用异常出现在 .NET 4.0 但不在 .NET 4.5