无法从xml文件反序列化为c#in.net 3.0 WCF服务中的对象

时间:2012-03-28 09:38:31

标签: c# wcf deserialization

我在WCF中有以下数据合约类:

[DataContract]
public class PropertyDetails
{
    # region >>---> private members

        string city;        
        string propertyType;       
        double price;        
        string propertyClass;  

    # endregion

    #region >>---> public properties
        [DataMember]
        public string City
        {
            get { return city; }
            set { city = value; }
        }

        [DataMember]
        public string PropertyType
        {
            get { return propertyType; }
            set { propertyType = value; }
        }

        [DataMember]
        public double Price
        {
            get { return price; }
            set { price = value; }
        }

        [DataMember]
        public string PropertyClass
        {
            get { return propertyClass; }
            set { propertyClass = value; }
        } 
    #endregion
}

}

我正在使用以下代码在XML文件中创建节点:

public void insertPropertyRecord(string city, string propertyType, double price, string propertyClass)
    {
        XmlTextReader propertyRecordReader = new XmlTextReader(WebConfigurationManager.AppSettings["XMLPropertyRecords"]);
        XmlDocument xmlDocPropertyRecords = new XmlDocument();
        xmlDocPropertyRecords.Load(propertyRecordReader);
        propertyRecordReader.Close();

        XmlElement xmlPropertyRecordRootNode = xmlDocPropertyRecords.DocumentElement;
        if (!xmlPropertyRecordRootNode.HasChildNodes)
        {
            XmlElement xmlPropertyRecordNode = xmlDocPropertyRecords.CreateElement("propertyrecord");
            XmlElement xmlPropertyRecordChildNode = null;

                xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("city");
                xmlPropertyRecordChildNode.InnerText = city;
                xmlPropertyRecordNode.AppendChild(xmlPropertyRecordChildNode);

                xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("propertytype");
                xmlPropertyRecordChildNode.InnerText = propertyType;
                xmlPropertyRecordNode.AppendChild(xmlPropertyRecordChildNode);

                xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("price");
                xmlPropertyRecordChildNode.InnerText = Convert.ToString(price);
                xmlPropertyRecordNode.AppendChild(xmlPropertyRecordChildNode);

                xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("propertyclass");
                xmlPropertyRecordChildNode.InnerText = propertyClass;
                xmlPropertyRecordNode.AppendChild(xmlPropertyRecordChildNode);

            xmlPropertyRecordRootNode.AppendChild(xmlPropertyRecordNode);
        }
        else
        {
            XmlElement xmlPropertyRecordLastNode = (XmlElement)xmlPropertyRecordRootNode.LastChild;
            XmlElement xmlPropertyRecordChildNode = null;
                XmlElement xmlPropertyRecordNode = xmlDocPropertyRecords.CreateElement("propertyrecord");

                    xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("city");
                    xmlPropertyRecordChildNode.InnerText = city;
                    xmlPropertyRecordChildNode.AppendChild(xmlPropertyRecordChildNode);

                    xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("propertytype");
                    xmlPropertyRecordChildNode.InnerText = propertyType;
                    xmlPropertyRecordChildNode.AppendChild(xmlPropertyRecordChildNode);

                    xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("price");
                    xmlPropertyRecordChildNode.InnerText = Convert.ToString(price);
                    xmlPropertyRecordChildNode.AppendChild(xmlPropertyRecordChildNode);

                    xmlPropertyRecordChildNode = xmlDocPropertyRecords.CreateElement("propertyclass");
                    xmlPropertyRecordChildNode.InnerText = propertyClass;
                    xmlPropertyRecordChildNode.AppendChild(xmlPropertyRecordChildNode);

            xmlPropertyRecordLastNode.AppendChild(xmlPropertyRecordNode);
        }
        xmlDocPropertyRecords.Save(WebConfigurationManager.AppSettings["XmlPropertyRecords"]);
    }

我正在尝试使用以下代码使用反序列化来获取对象中的节点:

XmlSerializer ser = new XmlSerializer(typeof(List<PropertyDetails>));
        TextReader sr = new StreamReader(WebConfigurationManager.AppSettings["XMLPropertyRecords"]);
        List<PropertyDetails> obj = new List<PropertyDetails>();
        obj = (List<PropertyDetails>)ser.Deserialize(sr);

但是因为无效操作异常而导致反序列化出错  创建的xml fie是以下ike:

<?xml version="1.0" encoding="utf-8"?>
<propertydetails>
  <propertyrecord>
    <City>delhi</City>
    <PropertyType>residential</PropertyType>
    <Price>4500000</Price>
    <PropertyClass>flat</PropertyClass>
  </propertyrecord>
</propertydetails>

请帮忙!

2 个答案:

答案 0 :(得分:0)

似乎XML状况不佳。尝试使用如下所述的XML。

<ArrayOfPropertydetails>
  <Propertydetails>
    <City>delhi</City>
    <PropertyType>residential</PropertyType>
    <Price>4500000</Price>
    <PropertyClass>flat</PropertyClass>
  </Propertydetails>
</ArrayOfPropertydetails>

答案 1 :(得分:0)

Well Formed“XML文档具有正确的XML语法。

语法规则在前面的章节中描述:  •XML文档必须具有根元素 •XML元素必须具有结束标记 •XML标记区分大小写 •XML元素必须正确嵌套 •必须引用XML属性值                   托弗       贾尼       提醒       这个周末别忘了我!