2017-12-17 8 views
0

アプリケーションは、データベースとして機能するxmlドキュメントにエントリを書き込みます。これらのエントリは、datagridviewの行として表示されます。各「エントリー」ノードには「entryno」属性があります。エントリが削除されるたびに、この "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(); 
    } 
FYIこれは、作成した新しいエントリ(それぞれ関連するコード)の「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ドキュメント。 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> 

ありがとうございました。

答えて

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++); 
      } 
     } 
    } 
} 
+0

私はカウント整数に値を属性にも「entryno」文字列を解析した後、Deletebtn_Clickイベントではなく、成功することなく、あなたのソリューションを実装しようとしています。うまくいけば、コードが "entryno"属性文字列のすべてを整数に変換してから、整数カウントを増やしてから各文字列の文字列値(文字列に再変換する)を返す必要があることは間違いないでしょう。属性。結果の整数をインクリメントする前に、属性文字列を解析してから、属性値として割り当てる文字列に再変換する必要があります。 TY。 – Diezus

+0

私は複雑にする必要がないものを複雑にしていました。あなたの解決策は正しいです。ありがとう。 – Diezus

関連する問題