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 中的命名空间引用

它在没有错误的情况下反序列化,但不填充 Items - 也就是说,“Items”为空。没有“东西”被填充

我应该在 xml 中引用 ns2 吗?如果是这样,怎么做?

4

1 回答 1

2

好的,通过更多的谷歌搜索得到了我的答案。

看起来您必须将 [XmlType(AnonymousType = true)] 添加到根,并将 [XmlElement(Form = 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; }                   
}
于 2013-02-01T16:07:37.723 回答