删除条目后,重置所有“entryno”属性的编号顺序

问题描述 投票:0回答:1

应用程序将条目写入xml文档 - 作为数据库 - 将这些条目显示为datagridview中的行。每个“入口”节点都有其“entryno”属性。每次删除一个条目时,这个“entryno”编号序列就会被打破。我能够实现代码来为每个创建的新条目增加“entryno”,但在尝试上述操作时已经卡住了一段时间。

private void Deletbtn_Click(object sender, EventArgs e)
    {
        XmlNode node = doc.SelectSingleNode("//entry[@entryno='" +     Dgv.CurrentRow.Cells[0].Value + "']");

        try
        {
            if (node != null && Dgv.CurrentRow.Selected == true)
            {
                doc.DocumentElement.RemoveChild(node);

                // Trying to refresh the sequence each time an node is deleted.
                int Attrno = int.Parse(doc.SelectSingleNode("//entry[@entryno]").Value);
                do
                {
                    Attrno = 0;
                    Attrno++;
                } while (Attrno < doc.ChildNodes.Count);
                doc.SelectSingleNode("//entry[@entryno]").InnerText = Attrno.ToString();

                doc.Save(Application.StartupPath + @"\SleepRecords.xml");
            }
            else
                MessageBox.Show("Click the left side of the grid to select a row to delete.", "Select row", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        ReloadData();
        Resetbtn_Click(Deletbtn, e);
        makePdf();
    }

仅供参考,这是我在每个创建的新条目(仅相关的代码段)上增加“entryno”属性的方法。

                XmlNodeList nodes = doc.SelectNodes("//entry");
            int max = 0;
            foreach (XmlNode node in nodes)
            {
                int nodeAttr = int.Parse(node.Attributes["entryno"].Value);
                max = nodeAttr;
            }
            max++;

存储条目数据的Xml Document。 datagridview从这里获取信息。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- These are the entries -->
<entries>
  <entry entryno="1">
    <weekday>Saturday</weekday>
    <date>16/12/2017</date>
    <time>21:42</time>
    <action>Out of bed</action>
    <mind>Ok</mind>
    <body>Ok</body>
  </entry>
  <entry entryno="2">
    <weekday>Sunday</weekday>
    <date>17/12/2017</date>
    <time>02:56</time>
    <action>Awake in bed</action>
    <mind>Ok</mind>
    <body>Ok</body>
  </entry>
  <entry entryno="3">
    <weekday>Sunday</weekday>
    <date>17/12/2017</date>
    <time>03:07</time>
    <action>Awakening</action>
    <mind>Ok</mind>
    <body>Ok</body>
  </entry>
  <entry entryno="4">
    <weekday>Sunday</weekday>
    <date>17/12/2017</date>
    <time>03:18</time>
    <action>Awakening</action>
    <mind>Ok</mind>
    <body>Ok</body>
  </entry>
  <entry entryno="5">
    <weekday>Sunday</weekday>
    <date>17/12/2017</date>
    <time>03:38</time>
    <action>Out of bed</action>
    <mind>Ok</mind>
    <body>Ok</body>
  </entry>
</entries>

谢谢你的帮助。

c# xml parsing datagridview increment
1个回答
0
投票

简单使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            int count = 1;
            foreach (XElement entry in doc.Descendants("entry"))
            {
                entry.SetAttributeValue("entryno", count++);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.