2013-01-31 71 views
1

我必須通過webrequest反序列化一些xml。我的反序列化器並沒有消除信息的肉 - 但它沒有發出錯誤。它所有的工作,如果我採取了上線的命名空間引用低於2使用命名空間反序列化會生成空項

XML(編輯隱藏的祕密業務的東西)

<?xml version="1.0" encoding="UTF-8"?> 
<ns2:thingees xmlns:ns2="http://someurl.com"> 
    <thing thing-code="KE"> 
    <thingelement description="primary" thing-address="address24000" sequence="1"/> 
    <thingelement description="backup" thing-address="address5000" sequence="2"/> 
    </thing> 
    <thing thing-code="PI"> 
    <thingelement description="primary" thing-address="address26000" sequence="1"/> 
    <thingelement description="backup" thing-address="address27000" sequence="2"/> 
    </thing> 
</ns2:thingees> 

我的課是如下(重命名的東西隱藏的祕密業務的東西)

[Serializable] 
[XmlRoot("thingees", Namespace = "http://someurl.com")] 
public class thingeeInfo 
{ 
    [XmlElementAttribute("thing")] 
    public oneThing[] Items {get; set;} 
} 


public partial class oneThing 
{ 
    [System.Xml.Serialization.XmlElementAttribute("thing-element")] 
    public ThingElement[] thingelement {get; set;} 

    [System.Xml.Serialization.XmlAttributeAttribute("thing-code")] 
    public string thingcode {get; set;} 

} 

public partial class ThingElement 
{ 


    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string description {get; set; } 

    [System.Xml.Serialization.XmlAttributeAttribute("thing-address")] 
    public string thingaddress {get; set; } 

    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string sequence {get; set; } 
} 

如果我 - 取出xml根目錄下的命名空間參考,它全部反序列化。 - 取出XMlRoot中的命名空間參考

它在沒有錯誤但沒有填充項目的情況下反序列化 - 也就是說,'項目'爲空。沒有'東西'填充

我應該引用ns2中的XML?如果是這樣,怎麼樣?

回答

2

好的,用更多的Google搜索得到了我的答案。

看起來必須添加[XmlType將(AnonymousType =真)]到根 和[的XmlElement(表格= XmlSchemaForm.Unqualified)]每個屬性和/或元件

// [Serializable]     
[Xmltype(AnonymousType = true)]  
[XmlRoot("thingees", Namespace = "http://someurl.com")] 
public class thingeeInfo 
{ 
    [XmlElementAttribute("thing", Form = XmlSchemaForm.Unqualified))]  
    public oneThing[] Items {get; set;} 
} 

public partial class oneThing 
{ 
    [XmlElementAttribute("thing-element", Form =XmlSchemaForm.Unqualified))] 
    public ThingElement[] thingelement {get; set;} 

    [XmlAttributeAttribute("thing-code", Form = XmlSchemaForm.Unqualified))] 
    public string thingcode {get; set;} 

} 

public partial class ThingElement 
{ 
    [XmlAttributeAttribute(Form = XmlSchemaForm.Unqualified))]      
    public string description {get; set; } 

    [XmlAttributeAttribute("thing-address", Form = XmlSchemaForm.Unqualified))]  
    public string thingaddress {get; set; } 

    [XmlAttributeAttribute(Form = XmlSchemaForm.Unqualified))]       
    public string sequence {get; set; }     
} 
相關問題