当前位置 : 主页 > 网页制作 > xml >

如何使用XML存储一个简单的对象(C#)

来源:互联网 收集:自由互联 发布时间:2021-06-13
我一直在使用如下文件来保存数据: field1 field2 field3 field4myname myhashedpass myemail@email.com more stuff afteretc etc etc etc 每行都转换为字符串(名称,通行证,电子邮件) 我想将我的文本文件(见上文
我一直在使用如下文件来保存数据:

field1 field2 field3 field4
myname myhashedpass myemail@email.com more stuff after
etc etc etc etc

每行都转换为字符串(名称,通行证,电子邮件)

我想将我的文本文件(见上文)转换为XML文件,如下所示:

<person1>
   <name>myname</name>
   <pass>myhashedpass</pass>
   <email>etc</email>
</person1>

<person2>
etc etc etc etc

基本上,我坚持如何进行此迁移,并以与处理文本数据相同的方式操纵XML数据

对你的问题的回答是这样的:

using System;
using System.Linq;
using System.Xml.Linq;

namespace XmlSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            var person1 = new Person();
            person1.Name = "Joe";
            person1.Password = "Cla$$ified";
            person1.Email = "none@your.bussiness";

            var person2 = new Person();
            person2.Name = "Doe";
            person2.Name = "$ecret";
            person2.Email = "dont@spam.me";

            var persons = new[] {person1, person2};

            XElement xml = new XElement("persons",
                                        from person in persons
                                        select new XElement("person",
                                                            new XElement("name", person.Name),
                                                            new XElement("password", person.Password),
                                                            new XElement("email", person.Email))
                                        );
            xml.Save("persons.xml");

            XElement restored_xml = XElement.Load("persons.xml");
            Person[] restored_persons = (from person in restored_xml.Elements("person")
                                         select new Person
                                                    {
                                                        Name = (string)person.Element("name"),
                                                        Password = (string)person.Element("password"),
                                                        Email = (string)person.Element("email")
                                                    })
                                        .ToArray();
            foreach (var person in restored_persons)
            {
                Console.WriteLine(person.ToString());
            }
            Console.ReadLine();
    }
}

public class Person
{
    public string Name { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public override string ToString()
    {
        return string.Format("The person with name {0} has password {1} and email {2}",
                             this.Name, this.Password, this.Email);
    }
}

}

但是,让内置的serializattion类为您进行XML的转换更好.下面的代码需要显式引用System.Runtime.Serialization.dll. using语句本身是不够的:

using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Runtime.Serialization;

namespace XmlSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            var person1 = new Person();
            person1.Name = "Joe";
            person1.Password = "Cla$$ified";
            person1.Email = "none@your.bussiness";

            var person2 = new Person();
            person2.Name = "Doe";
            person2.Name = "$ecret";
            person2.Email = "dont@spam.me";

            var persons = new[] {person1, person2};

            DataContractSerializer serializer=new DataContractSerializer(typeof(Person[]));
            using (var stream = new FileStream("persons.xml", FileMode.Create, FileAccess.Write))
            {
                serializer.WriteObject(stream,persons);
            }

            Person[] restored_persons;
            using (var another_stream=new FileStream("persons.xml",FileMode.Open,FileAccess.Read))
            {
                restored_persons = serializer.ReadObject(another_stream) as Person[];
            }

            foreach (var person in restored_persons)
            {
                Console.WriteLine(person.ToString());
            }
            Console.ReadLine();
        }
    }

    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string Email { get; set; }
        public override string ToString()
        {
            return string.Format("The person with name {0} has password {1} and email {2}",
                                 this.Name, this.Password, this.Email);
        }
    }
}
网友评论