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

本文关键字:System Xml XmlException 未处理 异常 类型 dll XML 发生了 | 更新日期: 2023-09-27 18:36:58

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中发生了类型为"System.Xml.XmlException"的未处理异常.dll 其他信息:缺少根元素。

请帮助我。


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

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

删除包含.xml文件的文件夹。然后用这个制作了一个新的.xml文件。

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

最简单的方法是使用 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; }
        }
    }
}