日期:2014-05-17  浏览次数:20766 次

怎么用泛型实现xml的功能?
老师留下作业就走了,对泛型不熟悉,没思路啊。。求指点,最好附加代码
public class XmlHelper
    {
        public List<T> LoadXml<T>(string path) { }
        public void SaveXml<T>(string path) { }
    }
C# xml 泛型

------解决方案--------------------
引用:
Quote: 引用:

http://blog.csdn.net/qiwenchao/article/details/7308822


大神。。这有点高深。。。小弟看不懂。。。


Google:MemoryStream 
Google:XmlSerializer 
Google:.............
任何看不懂的扔到Google的搜索框,会得到一些信息,对信息中的某些信息再不懂,继续Google~
不断地填补知识,然后就懂了~
------解决方案--------------------
今天哥心情好,给你写个完整能运行的作业,第二个save貌似还要加个参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace XmlRefl
{
class Program
{
static void Main(string[] args)
{

List<XCls> cls1 = new List<XCls>(){
new XCls()
{
Id = 12345,
Name = "X-Man",
}};

XmlHelper hlper = new XmlHelper();
hlper.SaveXml("xcls.xml", cls1);
Process.Start("xcls.xml");
cls1.Clear();
cls1 = hlper.LoadXml<XCls>("xcls.xml");
Console.WriteLine("Count=" + cls1.Count);
Console.ReadLine();
}
}

public class XCls
{
public int Id { get; set; }

public string Name { get; set; }
}

public class XmlHelper
{
public List<T> LoadXml<T>(string path)
{