首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# Xml --System.Xml.XmlException类型的未处理异常发生在System.Xml.dll中

C# Xml --System.Xml.XmlException类型的未处理异常发生在System.Xml.dll中
EN

Stack Overflow用户
提问于 2016-03-01 14:33:36
回答 2查看 5.2K关注 0票数 0
代码语言:javascript
运行
复制
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.XmlException‘类型异常,出现在System.Xml.dll中,附加信息:根元素丢失。

请帮帮我。

注释:异常是在这里引发的:xDoc.Load(path + "\\Address Book - Me \\settings.xml");

EN

回答 2

Stack Overflow用户

发布于 2016-05-24 14:28:56

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

代码语言:javascript
运行
复制
XmlTextWriter xW = new XmlTextWriter(YourPath, YourEncoding);
xW.WriteStartElement(Your Tag);
xW.WriteEndElement();
xW.Close(); 
票数 1
EN

Stack Overflow用户

发布于 2016-05-24 14:50:28

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

代码语言:javascript
运行
复制
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; }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35725646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档