序列化为类命名空间问题

时间:2013-10-30 16:28:31

标签: c# xml xml-serialization

所以我有这个XML字符串:

<?xml version="1.0" encoding="utf-16"?>
<options>
 <option category="Fabric" descriptiveName="BLUSH" grade="B">
    <optionProductID category="cover">
        <id optionIDQualifier="SellerAssigned" optionID="BLUSH" />
    </optionProductID>
    <optionQuantity unitOfMeasure="Each" value="1" />
    <optionPrice>899</optionPrice>
    <optionRetailPrice>899</optionRetailPrice>
    <sequenceNumber>0</sequenceNumber>
    <areaName>BODY</areaName>
    <areaCode />
 </option>
 <option category="Fabric" descriptiveName="597" grade="B">
    <optionProductID category="cover">
        <id optionIDQualifier="SellerAssigned" optionID="597" />
    </optionProductID>
    <optionQuantity unitOfMeasure="Each" value="1" />
    <optionPrice>20</optionPrice>
    <optionRetailPrice>20</optionRetailPrice>
    <sequenceNumber>0</sequenceNumber>
    <areaName>BACK CUSHION WELT</areaName>
    <areaCode />
 </option>
</options>

我创建了这些类:

 public class options
         {
            public List<option> option { get; set; }
         }

public class option
    {
                    [System.Xml.Serialization.XmlAttribute]
                    public string category { get; set; }
                    [System.Xml.Serialization.XmlAttribute]
                    public string descriptiveName { get; set; }
                    [System.Xml.Serialization.XmlAttribute]
                    public string grade { get; set; }
                    public string optionPrice { get; set; }
                    public string areaName { get; set; }
    }

我尝试将其反序列化:

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(options));
System.IO.StringReader rdr = new System.IO.StringReader(xmlstring);
options resultingMessage = (options)serializer.Deserialize(rdr);

我需要将这个XML字符串反序列化到这个类中但是仍然遇到问题,我在这里做错了什么?

目前我收到此错误:

<option xmlns=''> was not expected.

1 个答案:

答案 0 :(得分:0)

我认为类选项的实例名称应与根节点对象名称相同,

     public class options
     {
        public List<option> options { get; set; }
     }