0

我将一个 XML 文件绑定到一个转发器,每个项目都在一个文本框中。触发保存操作时,我需要将更改写回 XML 文件。我的问题是最有效的方法是什么,记住 XML 文件非常小。

我能想到三个,但我不知道哪个最好:

  1. 循环中继器,在 XML 文件中找到匹配的 ID,更新那些节点
  2. 循环 XML 文件,在中继器中找到匹配的 ID,更新那些节点
  3. 编写一个新的 XML 文件(替换旧的)

谢谢!

更新了我的 XML 文件的示例:

<?xml version="1.0" encoding="UTF-8"?>
<Link>
<LinkNode>
<Section>New York</Section>
<Header>New York</Header>
<Item>IT/ Phone Support</Item>
<Details>Ext 5.8115</Details>
<ID>0</ID>
<HyperLink>0</HyperLink>
</LinkNode>
<LinkNode>
<Section>New York</Section>
<Header />
<Item>Email</Item>
<Details>info@gld.com</Details>
<ID>1</ID>
<HyperLink>2</HyperLink>
</LinkNode>
</Link>

这是我的课:

public class Link
{
    public String Section { get; set; }
    public String Header { get; set; }
    public String Item { get; set; }
    public String Details { get; set; }
    public Int32 ID { get; set; }
    public Int32 Hyperlink { get; set; }
}

我的链接构造函数:

 var LinksList = new List<Links>();
 var query = links_xml.Root.Descendants("LinkNode").Select(d => d);

            foreach (var q in query)
            {



                var linklist = new Links((!string.IsNullOrEmpty(q.Element("Header").Value)) ? q.Element("Header").Value : "",
                    (!string.IsNullOrEmpty(q.Element("Item").Value)) ? q.Element("Item").Value : "",
                    (!string.IsNullOrEmpty(q.Element("Details").Value)) ? q.Element("Details").Value : "",
                    (int)q.Element("ID"), (int)q.Element("HyperLink"));
                LinksList.Add(linklist);

            }
4

2 回答 2

1

您还可以创建包含所需属性的类对象。将 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# 编程指南)

高温高压

于 2013-01-29T23:41:27.327 回答
0

使用 XmlDocument 加载 xml,然后在其中导航以找到包含要更改的值的元素。

System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(<filename or stream>);

Now, choose the most suitable method for you to find the elements or
attributes you want to change:

// For example:
XmlNodeList elemList = xmlDocument .GetElementsByTagName("Machine");
for (int i=0; i < elemList.Count; i++)
{
elemList[i].InnerXml = "Whateveryouwant";
}

然后,调用 Save 方法来保存更改。

xmlDocument.Save(...);

于 2013-01-29T22:21:23.280 回答