回答編集履歴
3
if文の判定条件ミス・・・
test
CHANGED
@@ -98,7 +98,7 @@
|
|
98
98
|
|
99
99
|
|
100
100
|
|
101
|
-
if (File.Exists(path))
|
101
|
+
if (!File.Exists(path))
|
102
102
|
|
103
103
|
{
|
104
104
|
|
2
XmlType
test
CHANGED
@@ -59,6 +59,8 @@
|
|
59
59
|
例えば、↓こんなクラスをつくり・・・
|
60
60
|
|
61
61
|
```csharp
|
62
|
+
|
63
|
+
[XmlType("data")]
|
62
64
|
|
63
65
|
public class Plan
|
64
66
|
|
1
デシリアライズするサンプル
test
CHANGED
@@ -47,3 +47,137 @@
|
|
47
47
|
|
48
48
|
|
49
49
|
```
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
### 実際のソースを見た後での追記
|
54
|
+
|
55
|
+
DataTableである必要があるのでしょうか?
|
56
|
+
|
57
|
+
やはりオブジェクトにデシリアライズするほうが、扱いやすく拡張もしやすいように思われます。
|
58
|
+
|
59
|
+
例えば、↓こんなクラスをつくり・・・
|
60
|
+
|
61
|
+
```csharp
|
62
|
+
|
63
|
+
public class Plan
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
[XmlElement("日時")]
|
68
|
+
|
69
|
+
public DateTime ScheduledDate { get; set; }
|
70
|
+
|
71
|
+
[XmlElement("場所")]
|
72
|
+
|
73
|
+
public string Place { get; set; }
|
74
|
+
|
75
|
+
[XmlElement("メモ")]
|
76
|
+
|
77
|
+
public string Note { get; set; }
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
↓こんなユーティリティクラスを用意しといて・・・
|
84
|
+
|
85
|
+
```csharp
|
86
|
+
|
87
|
+
public static class XmlUtility
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
public static T ConvertToObject<T>(string path, string xmlRoot = null) where T : class
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
T deserializedObject = default(T);
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
if (File.Exists(path))
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
return deserializedObject;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
XmlSerializer serializer = string.IsNullOrWhiteSpace(xmlRoot) ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), new XmlRootAttribute(xmlRoot));
|
110
|
+
|
111
|
+
using (var reader = new StreamReader(path))
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
deserializedObject = serializer.Deserialize(reader) as T;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
return deserializedObject;
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
↓こんな感じにする
|
130
|
+
|
131
|
+
```csharp
|
132
|
+
|
133
|
+
public partial class Display : Form
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
private string DataFileName = System.IO.Path.Combine(Application.StartupPath, "XMLFile1.xml");
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
public Display()
|
142
|
+
|
143
|
+
{
|
144
|
+
|
145
|
+
InitializeComponent();
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
private void Button_Click(object sender, EventArgs e)
|
152
|
+
|
153
|
+
{
|
154
|
+
|
155
|
+
// デシリアライズ
|
156
|
+
|
157
|
+
List<Plan> plans = XmlUtility.ConvertToObject<List<Plan>>(DataFileName, "DocumentElement");
|
158
|
+
|
159
|
+
if (plans != null)
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
// 現在から1週間分に絞り込み
|
164
|
+
|
165
|
+
DateTime start = DateTime.Now;
|
166
|
+
|
167
|
+
DateTime end = start.AddDays(7);
|
168
|
+
|
169
|
+
var thisWeekPlans = plans.Where(p => p.ScheduledDate.Between(start, end)).ToList();
|
170
|
+
|
171
|
+
// DataGridViewに表示したいと想像
|
172
|
+
|
173
|
+
dataGridView.DataSource = thisWeekPlans;
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
また、XMLの日時の書式は"YYYY-MM-DDThh:mm:ss"である必要があります。
|