在C#中反序列化时获取InvalidOperationException

本文关键字:获取 InvalidOperationException 反序列化 | 更新日期: 2023-09-27 18:27:24

我正在使用一个基于XML的.config文件来存储一些记录。我的XML如下:

    <?xml version="1.0" encoding="utf-8"?>
    <Data_List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Configuration>
        <Name>1st Week</Name>
        <Binary>
          <Field>field1</Field>
          <Version>1.0</Version>
        </Binary>
        <Binary>
          <Field>field2</Field>
          <Version>2.0</Version>
        </Binary>
    </Configuration>
      <Configuration>
        <Name>2nd Week</Name>
        <Binary>
          <Field>field1</Field>
          <Version>2.0</Version>
        </Binary>
        <Binary>
          <Field>field2</Field>
          <Version>4.0</Version>
        </Binary>
    </Configuration>
</Data_List>

我使用的C#代码如下:

public Binary
{
public String Field;
public String Version;
}
public Configuration
{
public String Name;
public List<Binary> Binary_List = new List<Binary>();
public GetfromXML()
{
List<Configuration> lists = new List<Configuration>();
TextReader reader = new StreamReader("Data_List.config");
XmlSerializer serializer = new XmlSerializer(typeof(List<Configuration>));
lists=(List<Configuration>)serializer.Deserialize(reader);
reader.Close();
}

我收到一个异常,说"XML文档(2,2)中有错误"有人能帮忙吗?

在C#中反序列化时获取InvalidOperationException

我认为问题在于您的模型没有很好地结构化。换句话说,序列化程序不知道如何读取.xml.

您的xml是错误的。当您拥有List<会有一个:

    <ArrayOfT></ArrayOfT>

在.XML中。以下是您需要的操作方法!

  1. 首先,尝试使用System.xml.Serialization(即[XmlArray()])中的xml属性

  2. 最好使用FileStream,而不是仅仅指出URI

     using(var filestream = new FileStream(//your uri, FIleMode.Open)
    {
    }
    
  3. 使用属性而不是变量。因为以后你可能想绑定

我如何设法解决这个问题的代码示例:

public ServiceMap Deserialize()
    {
        ServiceMap serviceMap = new ServiceMap();
        try
        {
            using (var fileStream = new FileStream(Settings.ServiceMapPath, FileMode.Open))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                using (XmlReader reader = XmlReader.Create(fileStream, settings))
                {
                    serviceMap = _serializer.Deserialize(reader) as ServiceMap;
                }
            }
        }
        catch (FileNotFoundException)
        {
            MessageBox.Show("File 'ServiceMap.xml' could not be found!");
        }
        return serviceMap;
    }

我的ServiceMap类:

    [XmlRoot("ServiceMap")]
public class ServiceMap
{
    [XmlArray("Nodes")]
    [XmlArrayItem("Node")]
    public List<Node> Nodes = new List<Node>();
    [XmlArray("Groups")]
    [XmlArrayItem("Group")]
    public List<Group> Groups = new List<Group>();
    [XmlArray("Categories")]
    [XmlArrayItem("Category")]
    public List<Category> Categories = new List<Category>();
}

编辑:我的XML:

<?xml version="1.0" encoding="utf-8"?>
<ServiceMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://   www.w3.org/2001/XMLSchema">
  <Nodes>
    <Node Name="Predrag">
  <Children>
    <Child>dijete1</Child>
    <Child>dijete2</Child>
    <Child>dijete3</Child>
    <Child>dijete4</Child>
  </Children>
  <Parents>
    <Parent>roditelj1</Parent>
    <Parent>roditelj2</Parent>
    <Parent>roditelj3</Parent>
  </Parents>
  <Group Name="Grupa" />
  <Category Name="Kategorija" />
</Node>
<Node Name="Tami">
  <Children>
    <Child>dijete1</Child>
    <Child>dijete2</Child>
  </Children>
  <Parents>
    <Parent>roditelj1</Parent>
  </Parents>
  <Group Name="Grupa2" />
  <Category Name="Kategorija2" />
</Node>