【问题标题】:C# Xml An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dllC# Xml System.Xml.dll 中出现“System.Xml.XmlException”类型的未处理异常
【发布时间】:2016-03-01 14:33:36
【问题描述】:
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");

【问题讨论】:

  • 错误试图告诉您,您的 XML 无效。

标签: c# xml contacts formclosing


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      相关资源
      最近更新 更多