xml数据读写 [英] xml data write and read

查看:69
本文介绍了xml数据读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将来自用户的数据保存到xml文件中以及如何向用户显示相同的数据?

how to save data taken from user in xml file and how to display same data to the user???

推荐答案

阅读下面的文章

http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTasp /a> [ ^ ]

http://www.dotnetperls.com/xmlwriter [
Read below article

http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx[^]

http://www.dotnetperls.com/xmlwriter[^]


我喜欢使用XML可序列化的类,因为它非常易于维护和修改.只需在类和属性上方添加属性.然后,您可以序列化为XML,然后将XML反序列化为对象.

示例:


I like to use an XML serializable class since it is very easy to maintain and modify. Just add the attributes above the class and properties. Then you can just serialize to an XML and deserialize your XML to an object.

Example:


using System;
using System.IO;
using System.Xml.Serialization;

    [XmlRoot("UserProfile")]
    public class Profile
    {
        [XmlElement()]
        public string Name { get; set; }
        [XmlElement()]
        public int Age { get; set; }
        
        public void Serialize(string uri)
        {
            if (string.IsNullOrEmpty(uri))
                throw new ArgumentNullException("File path invalid.");

            XmlSerializer s = new XmlSerializer(typeof(Profile));
            using (TextWriter w = new StreamWriter(uri))
            {
                s.Serialize(w, this);
                w.Close();
            }
        }

        public static Profile Deserialize(string uri)
        {
            if (string.IsNullOrEmpty(uri))
                throw new ArgumentNullException("File path invalid.");

            using (TextReader r = new StreamReader(uri))
            {
                XmlSerializer s = new XmlSerializer(typeof(Profile));
                return (Profile)s.Deserialize(r);   
            }
        }
    }


这篇关于xml数据读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆