回答編集履歴

2

見直しキャンペーン中

2023/08/15 15:31

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  書き込み以前に、要素(`XElement`)の取得ができていません。
5
5
 
6
- Linqは便利ですがエラーが出たときにどこで失敗しているかがわかりにくいです。
6
+ LINQは便利ですがエラーが出たときにどこで失敗しているかがわかりにくいです。
7
7
  適当に勘であれこれするのではなく、分解して一つずつ確認してください。
8
8
 
9
9
  ```cs
@@ -118,7 +118,7 @@
118
118
  }
119
119
  ```
120
120
 
121
- Linqに不慣れなら`foreach`で書いたほうがいいかもしれません。
121
+ LINQに不慣れならforeachで書いたほうがいいかもしれません。
122
122
  ```cs
123
123
  using System.Xml.Linq;
124
124
 

1

見直しキャンペーン中

2023/07/28 15:05

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,305 +1,153 @@
1
1
  > xmlファイルの読み込みまではうまくいくが、書き込みの際に例外エラーが発生し、
2
-
3
2
  > xmlファイルへの書き込みが反映されない状態となっています。
4
-
5
-
6
3
 
7
4
  書き込み以前に、要素(`XElement`)の取得ができていません。
8
5
 
9
-
10
-
11
6
  Linqは便利ですがエラーが出たときにどこで失敗しているかがわかりにくいです。
12
-
13
7
  適当に勘であれこれするのではなく、分解して一つずつ確認してください。
14
8
 
15
-
16
-
17
- ```C#
9
+ ```cs
18
-
19
10
  using System;
20
-
21
11
  using System.Linq;
22
-
23
12
  using System.Xml.Linq;
24
13
 
25
-
26
-
27
14
  namespace Questions352772
28
-
29
15
  {
30
-
31
16
  internal class Program
32
-
33
17
  {
34
-
35
18
  private static void Main()
36
-
37
19
  {
38
-
39
20
  var xml = XElement.Load(@"Ribbon1.xml");
40
21
 
41
-
42
-
43
22
  // まずは分解して一個ずつ確かめていくぞー
44
-
45
23
  var a = xml.Elements("button");
46
-
47
24
  Console.WriteLine(a.Count()); // 0
48
25
 
49
-
50
-
51
26
  // あれー取れないなぁ?
52
-
53
27
  // そもそもElementsってなんだっけ?
54
-
55
28
  var aa = xml.Elements();
56
-
57
29
  Console.WriteLine(aa.Count()); // 1
58
30
 
59
-
60
-
61
31
  // 1!? どういうこと?
62
-
63
32
  // リファレンス見てみよ
64
-
65
33
  // [XElement クラス (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xelement?view=netframework-4.7.2)
66
-
67
34
  // [XContainer.Elements メソッド(System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xcontainer.elements?view=netframework-4.7.2#System_Xml_Linq_XContainer_Elements_System_Xml_Linq_XName_)
68
-
69
35
  //ああそうか直接の子だけなのか...
70
36
 
71
-
72
-
73
37
  // 子孫を取得する方法を調べよー
74
-
75
38
  // これっぽいね
76
-
77
39
  // [XContainer.Descendants メソッド(System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xcontainer.descendants?view=netframework-4.7.2#System_Xml_Linq_XContainer_Descendants_System_Xml_Linq_XName_)
78
40
 
79
-
80
-
81
41
  var b = xml.Descendants();
82
-
83
42
  Console.WriteLine(b.Count()); // 5
84
43
 
85
-
86
-
87
44
  // うんうん予想通り!
88
-
89
45
  // じゃあbutton
90
46
 
91
-
92
-
93
47
  var bb = xml.Descendants("button");
94
-
95
48
  Console.WriteLine(bb.Count()); // 0
96
49
 
97
-
98
-
99
50
  // なんでやねん!
100
-
101
51
  // ggる 「xelement descendants not working」
102
-
103
52
  // [c# - XDocument.Descendants not returning descendants - Stack Overflow](https://stackoverflow.com/questions/11933782/xdocument-descendants-not-returning-descendants)
104
-
105
53
  // なるほど名前空間をつけないといけないのね(めんどくさ)
106
54
 
107
-
108
-
109
55
  XNamespace ns = "http://schemas.microsoft.com/office/2009/07/customui";
110
-
111
56
  var bbb = xml.Descendants(ns + "button");
112
-
113
57
  Console.WriteLine(bbb.Count()); // 1
114
58
 
115
-
116
-
117
59
  // よしよしここまではOK!
118
-
119
60
  // じゃあlabelも
120
61
 
121
-
122
-
123
62
  var c = from item in xml.Descendants(ns + "button")
124
-
125
63
  where item.Attributes("label").ToString() == "test"
126
-
127
64
  select item;
128
-
129
65
  Console.WriteLine(c.Count()); // 0
130
66
 
131
-
132
-
133
67
  // だめかぁorz
134
-
135
68
  // そういえばAttributesもよくわかってないなー
136
69
 
137
-
138
-
139
70
  var button = xml.Descendants(ns + "button").First();
140
-
141
71
  foreach (var att in button.Attributes())
142
-
143
72
  {
144
-
145
73
  Console.WriteLine(att.ToString());
146
-
147
74
  //id = "btniId"
148
-
149
75
  //label = "test"
150
-
151
76
  //onAction = "click_Event"
152
-
153
77
  }
154
78
 
155
-
156
-
157
79
  // あっ そうだよねToStringじゃおかしいよね
158
-
159
80
  // もっといいのがあるはず
160
-
161
81
  // [XAttribute クラス (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xattribute?view=netframework-4.7.2)
162
-
163
82
  // [XAttribute.Value プロパティ (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xattribute.value?view=netframework-4.7.2#System_Xml_Linq_XAttribute_Value)
164
-
165
83
  // 普通にValueあったやん
166
84
 
167
-
168
-
169
85
  // ええと。。こうか?
170
-
171
86
  //var cc = from item in xml.Descendants(ns + "button")
172
-
173
87
  // where item.Attributes("label").Value == "test"
174
-
175
88
  // select item;
176
89
 
177
-
178
-
179
90
  // コンパイルエラー
180
-
181
91
  // そうだよな複数形(Attributes)だもんな
182
-
183
92
  // えっと。。こうかな??
184
-
185
93
  var cc = from item in xml.Descendants(ns + "button")
186
-
187
94
  from att in item.Attributes("label")
188
-
189
95
  where att.Value == "test"
190
-
191
96
  select item;
192
-
193
97
  Console.WriteLine(c.Count()); // 1
194
98
 
195
-
196
-
197
99
  // ふぅやっとできたっぽいぞー
198
-
199
100
  // ほんとにダイジョブかテストをしとこう
200
-
201
-
202
-
203
101
 
204
102
 
205
103
  // xmlのbuttonの行を増やす・labelの文字を変える等 あってるかのテスト
206
104
 
207
105
 
208
-
209
-
210
-
211
106
  // よーしだいじょぶそう
212
-
213
107
  // で、なにやってたんだっけ?w
214
-
215
108
  // あぁそうそう
216
-
217
109
  var info = (from item in xml.Descendants(ns + "button")
218
-
219
110
  from att in item.Attributes("label")
220
-
221
111
  where att.Value == "test"
222
-
223
112
  select item).Single(); // Singleは必ず1つだけ該当するという前提ならOK
224
113
 
225
-
226
-
227
114
  info.Attribute("label").Value = "aaaaa";
228
-
229
115
  xml.Save(@"Ribbon1.xml");
230
-
231
116
  }
232
-
233
117
  }
234
-
235
118
  }
236
-
237
119
  ```
238
120
 
239
-
240
-
241
121
  Linqに不慣れなら`foreach`で書いたほうがいいかもしれません。
242
-
243
- ```C#
122
+ ```cs
244
-
245
123
  using System.Xml.Linq;
246
124
 
247
-
248
-
249
125
  namespace Questions352772
250
-
251
126
  {
252
-
253
127
  internal class Program
254
-
255
128
  {
256
-
257
129
  private static void Main()
258
-
259
130
  {
260
-
261
131
  var xml = XElement.Load(@"Ribbon1.xml");
262
-
263
132
  XNamespace ns = "http://schemas.microsoft.com/office/2009/07/customui";
264
133
 
265
-
266
-
267
134
  foreach (var button in xml.Descendants(ns + "button"))
268
-
269
135
  {
270
-
271
136
  foreach (var att in button.Attributes("label"))
272
-
273
137
  {
274
-
275
138
  if (att.Value == "test")
276
-
277
139
  {
278
-
279
140
  button.Attribute("label").Value = "aaaaa";
280
-
281
141
  xml.Save(@"Ribbon1.xml");
282
-
283
142
  return;
284
-
285
143
  }
286
-
287
144
  }
288
-
289
145
  }
290
-
291
146
  }
292
-
293
147
  }
294
-
295
148
  }
296
-
297
149
  ```
298
-
299
-
300
150
 
301
151
  > OnActionというイベントも動的に追加したいと考えています。
302
152
 
303
-
304
-
305
153
  VSTOについてはなにもわかりません^^;