无法从xml文件反序列化

时间:2015-01-13 08:33:18

标签: c# xmlreader xmlserializer

解释。反序列化时获取异常。任何人都可以帮我解决出错的原因。我已经为要从xml文件中提取的数据创建了一个类。但是,当反序列化回到对象时,我会得到异常。

<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<n1:GetPlugStatusListResponse xmlns:n1="http://tempuri.org/SPSuiteWebService/StandService">
  <n1:GetPlugStatusListResponse xmlns:n2="http://tempuri.org/GetPlugStatusListResponse.xsd">
<n2:StoreList>
<n2:Store>  
<n2:AreaType>13</n2:AreaType>
<n2:AreaName>東京都</n2:AreaName>
<n2:StoreCd>91110002</n2:StoreCd>
</n2:Store>    
<n2:Store>
</n2:Store>
 ...
</n2:StoreList>




    public static List<Store> ParseStoreList(string fileName) 
      {

          // Init
          List<Store> listStoreList = new List<Store>();

              // Parse...
              using (XmlReader xmlReader = XmlReader.Create(fileName))
              {
                  XmlSerializer EventSerializer = new XmlSerializer(typeof(Store));


                  while (xmlReader.ReadToFollowing("n2:Store"))
                  {
                      Store storeObject = (Store)EventSerializer.Deserialize(xmlReader.ReadSubtree());
                      listStoreList.Add(storeObject);

                  }

                  xmlReader.Close();
              }




          // Return value
          return listStoreList;

      }

    public static List<PlugListData> ParsePlugList(string fileName)
    {
        // Init
        List<PlugListData> listPlugList = new List<PlugListData>();

        // Parse...
        using (XmlReader xmlReader = XmlReader.Create(fileName))
        {
            XmlSerializer EventSerializer = new XmlSerializer(typeof(Store));

            // Parse XML - "Event" nodes...
            while (xmlReader.ReadToFollowing("n2:PlugStatus"))
            {
                PlugListData pluglistObject = (PlugListData)EventSerializer.Deserialize(xmlReader.ReadSubtree());
                listPlugList.Add(pluglistObject);
            }

            // Cleanup...
            xmlReader.Close();
        }

        // Return value
        return listPlugList;
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试以下

XmlSerializer deserializer = new XmlSerializer(typeof(Store));// create the serializer
TextReader reader = new StreamReader("n2:Store"); //read your text
object obj = deserializer.Deserialize(reader);// create object

Store xmlData = (Store)obj;// access data from object