C#Xml System.Xml.dll中发生了未处理的“System.Xml.XmlException”类型的异常

时间:2016-03-01 14:33:37

标签: c# xml contacts formclosing

private void Form1_FormClosing(object sender, FormClosingEventArgs e) //Save On Form Closing
{
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(path + "\\Address Book - Me \\settings.xml");
    XmlNode xNode = xDoc.SelectSingleNode("People");
    xNode.RemoveAll();
    foreach (Person pe in people)
    {
        XmlNode xTop = xDoc.CreateElement("People");
        XmlNode xName = xDoc.CreateElement("Name");
        XmlNode xLastName = xDoc.CreateElement("LastName");
        XmlNode xStreet = xDoc.CreateElement("Address");
        XmlNode xPhone = xDoc.CreateElement("Phone");
        XmlNode xEmail = xDoc.CreateElement("Email");
        XmlNode xDate = xDoc.CreateElement("Birth");
        XmlNode xCity = xDoc.CreateElement("City");
        XmlNode xState = xDoc.CreateElement("State");
        XmlNode xCountry = xDoc.CreateElement("Country");
        XmlNode xDetails = xDoc.CreateElement("Detail");
        xName.InnerText = pe.Name;
        xLastName.InnerText = pe.LastName;
        xStreet.InnerText = pe.StreetAdress;
        xPhone.InnerText = pe.Phone;
        xEmail.InnerText = pe.Email;
        xDate.InnerText = pe.Date.ToFileTime().ToString();
        xCity.InnerText = pe.City;
        xState.InnerText = pe.State;
        xCountry.InnerText = pe.Country;
        xDetails.InnerText = pe.Details;
        xTop.AppendChild(xName);//adding a new node
        xTop.AppendChild(xLastName);
        xTop.AppendChild(xStreet);
        xTop.AppendChild(xPhone);
        xTop.AppendChild(xEmail);
        xTop.AppendChild(xDate);
        xTop.AppendChild(xCity);
        xTop.AppendChild(xState);
        xTop.AppendChild(xCountry);
        xTop.AppendChild(xDetails);
        xDoc.DocumentElement.AppendChild(xTop);
    }

    xDoc.Save(path + "\\Address Book - Me \\settings.xml");//

我正在尝试创建一个代理,它可以在我重新启动应用后保存信息并重新加载它们。但当我关闭我的程序时,没有任何作用,就是这样:

  

Xml System.Xml.dll中发生了一个未处理的“System.Xml.XmlException”类型异常并且附加信息:缺少根元素。

请帮帮我。

来自评论:此处抛出异常:xDoc.Load(path + "\\Address Book - Me \\settings.xml");

2 个答案:

答案 0 :(得分:1)

使用.xml文件删除您的文件夹。然后使用它创建一个新的.xml文件。

XmlTextWriter xW = new XmlTextWriter(YourPath, YourEncoding);
xW.WriteStartElement(Your Tag);
xW.WriteEndElement();
xW.Close(); 

答案 1 :(得分:0)

最简单的方法是使用XmlSerialization,如下例所示。

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            var settings = new Settings();
            settings.People.Add(new Person { Name = "Name", LastName = "LastName", City="City", Country="Country", Date="11/11/11", Details="Details", Email="Email", Phone="Phone", State="State", Street="Steet" });
            settings.Save("c:\\test.xml");
            settings = Settings.TryLoad("c:\\test.xml");
        }

        [Serializable]
        public class Settings
        {
            public Settings()
            {
            }

            public List<Person> People
            {
                get { return people; }
                set { people = value; }
            }
            List<Person> people = new List<Person>();

            public void Save(string path)
            {
                XmlSerializer xs = new XmlSerializer(typeof(Settings));
                using (var sw = new StreamWriter(File.Open(path, FileMode.OpenOrCreate)))
                {
                    xs.Serialize(sw, this);
                }
            }

            public static Settings TryLoad(string path)
            {
                Settings settings = null;
                XmlSerializer xs = new XmlSerializer(typeof(Settings));
                using (var sw = new StreamReader(File.OpenRead(path)))
                {
                    try
                    {
                        settings = xs.Deserialize(sw) as Settings;
                    }
                    catch (Exception)
                    {
                        // skip
                    }
                }
                return settings;
            }
        }

        [Serializable]
        public class Person
        {
            public Person()
            {
            }

            public string Name { get; set; }
            public string LastName { get; set; }
            public string Street { get; set; }
            public string Phone { get; set; }
            public string Email { get; set; }
            public string Date { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
            public string Details { get; set; }
        }
    }
}