如何在反序列化xml </configuration>时不会出现错误<configuration xmlns =“”>

时间:2014-04-27 02:45:52

标签: xml serialization

我有config xml:

<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Gift Perfect</Name>
  <Designs>

  </Designs>
  <Root>

  </Root>
  <FormElements>

  </FormElements>   
</Configuration>

ConfigurationModel:

[Serializable()]
    [XmlRoot("Configuration", Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = true)]
    public class ConfigurationModel
    {
        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Designs")]
        public string Designs { get; set; }

        [XmlElement("Root")]
        public string Root { get; set; }
    }

当我尝试通过此代码反序列化时:

var type = typeof(ConfigurationModel);
var s = new XmlSerializer(type);
var xml = string.Empty;

using (StreamReader reader = new StreamReader(Server.MapPath("~/xml/Config.xml")))
{
    xml = reader.ReadToEnd();
    reader.Close();
}

TextReader reader = new StringReader(xml);
ConfigurationModel o = (ConfigurationModel)s.Deserialize(reader);
reader.Close();

var result = o;

我收到错误:

System.InvalidOperationException: <Configuration xmlns=''> was not expected.

在线:

ConfigurationModel o = (ConfigurationModel)s.Deserialize(reader);

Plase帮助我找到解决方案。

0 个答案:

没有答案