2013-12-13 153 views
0

我一直在使用xml,并且陷入了保存/加载的方面。我想从应用程序关闭时将文本框中的信息保存到xml文件中,但是当应用程序运行时,我无法关闭带有右上角“X”的应用程序。我尝试删除xml文件并重新创建它,但这仍然没有帮助C#应用程序不保存为xml

我得到了应用程序关闭现在和保存功能与xml工作但是负载函数没有经过它的foreach循环来添加信息从xml回到应用

XmlDocument xDoc = new XmlDocument(); 
      xDoc.Load(path + place); 
      foreach(XmlNode xNode in xDoc.SelectNodes("ClientMeasurements\\Client")) 
      { 
       Client c = new Client(); 

       c.Name = xNode.SelectSingleNode("Name").InnerText; 
       c.InitialForearm = xNode.SelectSingleNode("InitialForearm").InnerText; 
       c.InitialUpperArmR = xNode.SelectSingleNode("InitialUpperArmRight").InnerText; 
       c.InitialUpperArmL = xNode.SelectSingleNode("InitialUpperArmLeft").InnerText; 
       c.InitialChest = xNode.SelectSingleNode("InitialChest").InnerText; 
       c.InitialWaist = xNode.SelectSingleNode("InitialWaist").InnerText; 
       c.InitialHips = xNode.SelectSingleNode("InitialHips").InnerText; 
       c.InitialThighR = xNode.SelectSingleNode("InitialThighRight").InnerText; 
       c.InitialThighL = xNode.SelectSingleNode("InitialThighLeft").InnerText; 
       c.InitialCalfR = xNode.SelectSingleNode("InitialCalfRight").InnerText; 
       c.InitialCalfL = xNode.SelectSingleNode("InitialCalfLeft").InnerText; 
       c.MostRecentForearm = xNode.SelectSingleNode("MostRecentForearm").InnerText; 
       c.MostRecentUpperArmR = xNode.SelectSingleNode("MostRecentUpperArmRight").InnerText; 
       c.MostRecentUpperArmL = xNode.SelectSingleNode("MostRecentUpperArmLeft").InnerText; 
       c.MostRecentChest = xNode.SelectSingleNode("MostRecentChest").InnerText; 
       c.MostRecentWaist = xNode.SelectSingleNode("MostRecentWaist").InnerText; 
       c.MostRecentHips = xNode.SelectSingleNode("MostRecentHips").InnerText; 
       c.MostRecentThighR = xNode.SelectSingleNode("MostRecentThighRight").InnerText; 
       c.MostRecentThighL = xNode.SelectSingleNode("MostRecentThighLeft").InnerText; 
       c.MostRecentCalfR = xNode.SelectSingleNode("MostRecentCalfRight").InnerText; 
       c.MostRecentCalfL = xNode.SelectSingleNode("MostRecentCalfLeft").InnerText; 

       client.Add(c); 
       listView1.Items.Add(c.Name); 
      } 
     } 
+0

你是什​​么意思,你不能点击关闭按钮?它变灰了吗?点击它时它会挂起吗? – mclaassen

+0

当我点击它时,该应用程序无响应。它不灰色,按钮点击,但不会关闭应用程序。 –

+1

您是否尝试过放置断点并查看哪行代码或方法调用令人不安? – TheVillageIdiot

回答

1

还有一个更简单的方法来做到这一点使用XML序列化。

Here是一篇文章,让你开始。

基本上你想[Serializeable]来装饰你的客户端类,然后使用这样的保存和加载你的clients对象(我假设是一些集合类)从XML文件:

public static void SerializeObject<T>(T serializableObject, string fileName) 
    { 
     if (serializableObject == null) { return; } 

     try 
     { 
      XmlDocument xmlDocument = new XmlDocument(); 
      XmlSerializer serializer = new XmlSerializer(serializableObject.GetType()); 
      using (MemoryStream stream = new MemoryStream()) 
      { 
       serializer.Serialize(stream, serializableObject); 
       stream.Position = 0; 
       xmlDocument.Load(stream); 
       xmlDocument.Save(fileName); 
       stream.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      //Log exception here 
     } 
    } 

    public static T DeSerializeObject<T>(string fileName) 
    { 
     if (string.IsNullOrEmpty(fileName)) { return default(T); } 

     T objectOut = default(T); 

     try 
     { 
      string attributeXml = string.Empty; 

      XmlDocument xmlDocument = new XmlDocument(); 
      xmlDocument.Load(fileName); 
      string xmlString = xmlDocument.OuterXml; 

      using (StringReader read = new StringReader(xmlString)) 
      { 
       Type outType = typeof(T); 

       XmlSerializer serializer = new XmlSerializer(outType); 
       using (XmlReader reader = new XmlTextReader(read)) 
       { 
        objectOut = (T)serializer.Deserialize(reader); 
        reader.Close(); 
       } 

       read.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      //Log exception here 
     } 

     return objectOut; 
    } 
+0

谢谢你的答复,但我想知道代码现在的方式有什么问题,以防止它保存/加载以备将来参考 –

相关问题