您还可以创建包含所需属性的类对象。将 XML 的读写放在一个 DAL 层中。这样,您的 DAL 传递和接收List<Url>
这些对象中的一个,并且您从应用程序的其余部分抽象数据访问(在您的情况下为 XML)。
如果您选择更改数据存储方法(更改为 SQL 等),您只需更改 DAL 方法,其余代码不受影响。
如果您想要一些代码来说明,请告诉我。
编辑:(一些代码)
假设您的元素具有以下属性,这将是您的类:
public class Url
{
public int Id {get; set;}
public string DisplayName {get; set;}
public string Url {get; set;}
}
然后您的 DAL 类将如下所示:
/// <summary>
/// Handles data access for Url entities
/// </summary>
public static class UrlDAL
{
/// <summary>
/// The path where the file resides
/// </summary>
const string __FilePath = @"~\App_Data\";
/// <summary>
/// The name of the file
/// </summary>
const string __FileName = "UrlList.xml";
/// <summary>
/// The name of the temporary file. Used to keep a copy of the file if saving the new file fails.
/// </summary>
const string __FileNameTemp = "UrlList_TEMP.xml";
/// <summary>
/// Retrieves a list of url entity objects
/// </summary>
/// <returns></returns>
internal static List<Url> Retrieve()
{
List<Url> urls = null;
try
{
//read xml file
XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath(Path.Combine(UrlDAL.__FilePath, UrlDAL..__FileName)));
//convert to list
urls = Xml.DeserializeCollection<Url>(data);
}
catch (Exception e)
{
//perform logging / error handling
}
return urls;
}
/// <summary>
/// Saves the list of Url entities
/// </summary>
/// <param name="urls">The list of url objects to save</param>
internal static void Create(List<Url> urls)
{
try
{
//convert list into xml document
XDocument file = Xml.SerializeCollection<Url>(urls);
//rename the old file so you have a copy of it if saving the new file fails
File.Copy(Path.Combine(UrlDAL.__FilePath, UrlDAL.__FileName), Path.Combine(UrlDAL.__FilePath, UrlDAL.__FileNameTemp));
//saving the new file will overwrite the existing file
file.Save(UrlDAL.__FilePath);
//delete the old file
File.Delete(Path.Combine(UrlDAL.__FilePath, UrlDAL.__FileNameTemp));
}
catch (Exception e)
{
//perform logging / error handling
//you should also alert yourself of this error so you can manually restore the old file
}
}
}
这些是用于 XML 序列化/反序列化的扩展方法:
public static class Xml
{
/// <summary>
/// Converts an XDoc into a List<T> of entities
/// </summary>
/// <typeparam name="T">Any serializable object</typeparam>
/// <param name="doc"></param>
/// <returns></returns>
public static List<T> DeserializeCollection<T>(XDocument doc)
{
if (doc == null)
return null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
XmlReader reader = doc.CreateReader();
List<T> result = (List<T>)serializer.Deserialize(reader);
reader.Close();
return result;
}
catch (Exception e)
{
//perform logging / error handling
return null;
}
}
/// <summary>
/// Converts a List<T> of entities into an XDoc.
/// </summary>
/// <typeparam name="T">Any serializable object</typeparam>
/// <param name="paramList"></param>
public static XDocument SerializeCollection<T>(List<T> paramList)
{
if (paramList == null)
return null;
XDocument doc = new XDocument();
try
{
XmlSerializer serializer = new XmlSerializer(paramList.GetType());
XmlWriter writer = doc.CreateWriter();
serializer.Serialize(writer, paramList);
writer.Close();
return doc;
}
catch (Exception e)
{
//perform logging / error handling
return null;
}
}
}
不要忘记添加对以下命名空间的引用:
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
话虽如此,你会这样称呼 DAL:
//to retrieve the list of urls
List<Url> urls = UrlDAL.Retrieve();
//to save the new list of urls to an xml file
List<Url> urls = [BUILD URL ENTITIES FROM REPEATER];
UrlDAL.Create(urls)
编辑:创建 XML 的提示
为了避免反序列化 XML 时出现任何问题(XML 对于解析的内容可能非常挑剔),我倾向于在代码中创建一些(或全部)我想使用的类,并通过 Xml.Serialize 方法运行 List。然后,我使用在那里创建的 xml 作为创建 XML 文件的起点。
编辑从转发器创建实例
当您遍历转发器中的每个项目时,您可以像这样使用 .NET的对象初始化程序:
List<Links> links = new List<Links>();
foreach (RepeaterItem item in rptLinks.Items)
{
//find all your controls and their values
string details = ((TextBox)e.Item.Findcontrol("txtDetails")).Text;
//do this for each control
//use object initialization here
links.Add(new Link {Details = details, [PROPERTY NAME] = [REPEATER ITEM VALUE], etc});
}
可以在此处的 MSDN 上找到对此功能的一个很好的解释:对象和集合初始化程序(C# 编程指南)
高温高压