回答編集履歴
2
見直しキャンペーン中
answer
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
書き込み以前に、要素(`XElement`)の取得ができていません。
|
5
5
|
|
6
|
-
|
6
|
+
LINQは便利ですがエラーが出たときにどこで失敗しているかがわかりにくいです。
|
7
7
|
適当に勘であれこれするのではなく、分解して一つずつ確認してください。
|
8
8
|
|
9
9
|
```cs
|
@@ -118,7 +118,7 @@
|
|
118
118
|
}
|
119
119
|
```
|
120
120
|
|
121
|
-
|
121
|
+
LINQに不慣れならforeachで書いたほうがいいかもしれません。
|
122
122
|
```cs
|
123
123
|
using System.Xml.Linq;
|
124
124
|
|
1
見直しキャンペーン中
answer
CHANGED
@@ -1,153 +1,153 @@
|
|
1
|
-
> xmlファイルの読み込みまではうまくいくが、書き込みの際に例外エラーが発生し、
|
2
|
-
> xmlファイルへの書き込みが反映されない状態となっています。
|
3
|
-
|
4
|
-
書き込み以前に、要素(`XElement`)の取得ができていません。
|
5
|
-
|
6
|
-
Linqは便利ですがエラーが出たときにどこで失敗しているかがわかりにくいです。
|
7
|
-
適当に勘であれこれするのではなく、分解して一つずつ確認してください。
|
8
|
-
|
9
|
-
```
|
10
|
-
using System;
|
11
|
-
using System.Linq;
|
12
|
-
using System.Xml.Linq;
|
13
|
-
|
14
|
-
namespace Questions352772
|
15
|
-
{
|
16
|
-
internal class Program
|
17
|
-
{
|
18
|
-
private static void Main()
|
19
|
-
{
|
20
|
-
var xml = XElement.Load(@"Ribbon1.xml");
|
21
|
-
|
22
|
-
// まずは分解して一個ずつ確かめていくぞー
|
23
|
-
var a = xml.Elements("button");
|
24
|
-
Console.WriteLine(a.Count()); // 0
|
25
|
-
|
26
|
-
// あれー取れないなぁ?
|
27
|
-
// そもそもElementsってなんだっけ?
|
28
|
-
var aa = xml.Elements();
|
29
|
-
Console.WriteLine(aa.Count()); // 1
|
30
|
-
|
31
|
-
// 1!? どういうこと?
|
32
|
-
// リファレンス見てみよ
|
33
|
-
// [XElement クラス (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xelement?view=netframework-4.7.2)
|
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_)
|
35
|
-
//ああそうか直接の子だけなのか...
|
36
|
-
|
37
|
-
// 子孫を取得する方法を調べよー
|
38
|
-
// これっぽいね
|
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_)
|
40
|
-
|
41
|
-
var b = xml.Descendants();
|
42
|
-
Console.WriteLine(b.Count()); // 5
|
43
|
-
|
44
|
-
// うんうん予想通り!
|
45
|
-
// じゃあbutton
|
46
|
-
|
47
|
-
var bb = xml.Descendants("button");
|
48
|
-
Console.WriteLine(bb.Count()); // 0
|
49
|
-
|
50
|
-
// なんでやねん!
|
51
|
-
// ggる 「xelement descendants not working」
|
52
|
-
// [c# - XDocument.Descendants not returning descendants - Stack Overflow](https://stackoverflow.com/questions/11933782/xdocument-descendants-not-returning-descendants)
|
53
|
-
// なるほど名前空間をつけないといけないのね(めんどくさ)
|
54
|
-
|
55
|
-
XNamespace ns = "http://schemas.microsoft.com/office/2009/07/customui";
|
56
|
-
var bbb = xml.Descendants(ns + "button");
|
57
|
-
Console.WriteLine(bbb.Count()); // 1
|
58
|
-
|
59
|
-
// よしよしここまではOK!
|
60
|
-
// じゃあlabelも
|
61
|
-
|
62
|
-
var c = from item in xml.Descendants(ns + "button")
|
63
|
-
where item.Attributes("label").ToString() == "test"
|
64
|
-
select item;
|
65
|
-
Console.WriteLine(c.Count()); // 0
|
66
|
-
|
67
|
-
// だめかぁorz
|
68
|
-
// そういえばAttributesもよくわかってないなー
|
69
|
-
|
70
|
-
var button = xml.Descendants(ns + "button").First();
|
71
|
-
foreach (var att in button.Attributes())
|
72
|
-
{
|
73
|
-
Console.WriteLine(att.ToString());
|
74
|
-
//id = "btniId"
|
75
|
-
//label = "test"
|
76
|
-
//onAction = "click_Event"
|
77
|
-
}
|
78
|
-
|
79
|
-
// あっ そうだよねToStringじゃおかしいよね
|
80
|
-
// もっといいのがあるはず
|
81
|
-
// [XAttribute クラス (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xattribute?view=netframework-4.7.2)
|
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)
|
83
|
-
// 普通にValueあったやん
|
84
|
-
|
85
|
-
// ええと。。こうか?
|
86
|
-
//var cc = from item in xml.Descendants(ns + "button")
|
87
|
-
// where item.Attributes("label").Value == "test"
|
88
|
-
// select item;
|
89
|
-
|
90
|
-
// コンパイルエラー
|
91
|
-
// そうだよな複数形(Attributes)だもんな
|
92
|
-
// えっと。。こうかな??
|
93
|
-
var cc = from item in xml.Descendants(ns + "button")
|
94
|
-
from att in item.Attributes("label")
|
95
|
-
where att.Value == "test"
|
96
|
-
select item;
|
97
|
-
Console.WriteLine(c.Count()); // 1
|
98
|
-
|
99
|
-
// ふぅやっとできたっぽいぞー
|
100
|
-
// ほんとにダイジョブかテストをしとこう
|
101
|
-
|
102
|
-
|
103
|
-
// xmlのbuttonの行を増やす・labelの文字を変える等 あってるかのテスト
|
104
|
-
|
105
|
-
|
106
|
-
// よーしだいじょぶそう
|
107
|
-
// で、なにやってたんだっけ?w
|
108
|
-
// あぁそうそう
|
109
|
-
var info = (from item in xml.Descendants(ns + "button")
|
110
|
-
from att in item.Attributes("label")
|
111
|
-
where att.Value == "test"
|
112
|
-
select item).Single(); // Singleは必ず1つだけ該当するという前提ならOK
|
113
|
-
|
114
|
-
info.Attribute("label").Value = "aaaaa";
|
115
|
-
xml.Save(@"Ribbon1.xml");
|
116
|
-
}
|
117
|
-
}
|
118
|
-
}
|
119
|
-
```
|
120
|
-
|
121
|
-
Linqに不慣れなら`foreach`で書いたほうがいいかもしれません。
|
122
|
-
```
|
123
|
-
using System.Xml.Linq;
|
124
|
-
|
125
|
-
namespace Questions352772
|
126
|
-
{
|
127
|
-
internal class Program
|
128
|
-
{
|
129
|
-
private static void Main()
|
130
|
-
{
|
131
|
-
var xml = XElement.Load(@"Ribbon1.xml");
|
132
|
-
XNamespace ns = "http://schemas.microsoft.com/office/2009/07/customui";
|
133
|
-
|
134
|
-
foreach (var button in xml.Descendants(ns + "button"))
|
135
|
-
{
|
136
|
-
foreach (var att in button.Attributes("label"))
|
137
|
-
{
|
138
|
-
if (att.Value == "test")
|
139
|
-
{
|
140
|
-
button.Attribute("label").Value = "aaaaa";
|
141
|
-
xml.Save(@"Ribbon1.xml");
|
142
|
-
return;
|
143
|
-
}
|
144
|
-
}
|
145
|
-
}
|
146
|
-
}
|
147
|
-
}
|
148
|
-
}
|
149
|
-
```
|
150
|
-
|
151
|
-
> OnActionというイベントも動的に追加したいと考えています。
|
152
|
-
|
1
|
+
> xmlファイルの読み込みまではうまくいくが、書き込みの際に例外エラーが発生し、
|
2
|
+
> xmlファイルへの書き込みが反映されない状態となっています。
|
3
|
+
|
4
|
+
書き込み以前に、要素(`XElement`)の取得ができていません。
|
5
|
+
|
6
|
+
Linqは便利ですがエラーが出たときにどこで失敗しているかがわかりにくいです。
|
7
|
+
適当に勘であれこれするのではなく、分解して一つずつ確認してください。
|
8
|
+
|
9
|
+
```cs
|
10
|
+
using System;
|
11
|
+
using System.Linq;
|
12
|
+
using System.Xml.Linq;
|
13
|
+
|
14
|
+
namespace Questions352772
|
15
|
+
{
|
16
|
+
internal class Program
|
17
|
+
{
|
18
|
+
private static void Main()
|
19
|
+
{
|
20
|
+
var xml = XElement.Load(@"Ribbon1.xml");
|
21
|
+
|
22
|
+
// まずは分解して一個ずつ確かめていくぞー
|
23
|
+
var a = xml.Elements("button");
|
24
|
+
Console.WriteLine(a.Count()); // 0
|
25
|
+
|
26
|
+
// あれー取れないなぁ?
|
27
|
+
// そもそもElementsってなんだっけ?
|
28
|
+
var aa = xml.Elements();
|
29
|
+
Console.WriteLine(aa.Count()); // 1
|
30
|
+
|
31
|
+
// 1!? どういうこと?
|
32
|
+
// リファレンス見てみよ
|
33
|
+
// [XElement クラス (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xelement?view=netframework-4.7.2)
|
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_)
|
35
|
+
//ああそうか直接の子だけなのか...
|
36
|
+
|
37
|
+
// 子孫を取得する方法を調べよー
|
38
|
+
// これっぽいね
|
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_)
|
40
|
+
|
41
|
+
var b = xml.Descendants();
|
42
|
+
Console.WriteLine(b.Count()); // 5
|
43
|
+
|
44
|
+
// うんうん予想通り!
|
45
|
+
// じゃあbutton
|
46
|
+
|
47
|
+
var bb = xml.Descendants("button");
|
48
|
+
Console.WriteLine(bb.Count()); // 0
|
49
|
+
|
50
|
+
// なんでやねん!
|
51
|
+
// ggる 「xelement descendants not working」
|
52
|
+
// [c# - XDocument.Descendants not returning descendants - Stack Overflow](https://stackoverflow.com/questions/11933782/xdocument-descendants-not-returning-descendants)
|
53
|
+
// なるほど名前空間をつけないといけないのね(めんどくさ)
|
54
|
+
|
55
|
+
XNamespace ns = "http://schemas.microsoft.com/office/2009/07/customui";
|
56
|
+
var bbb = xml.Descendants(ns + "button");
|
57
|
+
Console.WriteLine(bbb.Count()); // 1
|
58
|
+
|
59
|
+
// よしよしここまではOK!
|
60
|
+
// じゃあlabelも
|
61
|
+
|
62
|
+
var c = from item in xml.Descendants(ns + "button")
|
63
|
+
where item.Attributes("label").ToString() == "test"
|
64
|
+
select item;
|
65
|
+
Console.WriteLine(c.Count()); // 0
|
66
|
+
|
67
|
+
// だめかぁorz
|
68
|
+
// そういえばAttributesもよくわかってないなー
|
69
|
+
|
70
|
+
var button = xml.Descendants(ns + "button").First();
|
71
|
+
foreach (var att in button.Attributes())
|
72
|
+
{
|
73
|
+
Console.WriteLine(att.ToString());
|
74
|
+
//id = "btniId"
|
75
|
+
//label = "test"
|
76
|
+
//onAction = "click_Event"
|
77
|
+
}
|
78
|
+
|
79
|
+
// あっ そうだよねToStringじゃおかしいよね
|
80
|
+
// もっといいのがあるはず
|
81
|
+
// [XAttribute クラス (System.Xml.Linq) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xattribute?view=netframework-4.7.2)
|
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)
|
83
|
+
// 普通にValueあったやん
|
84
|
+
|
85
|
+
// ええと。。こうか?
|
86
|
+
//var cc = from item in xml.Descendants(ns + "button")
|
87
|
+
// where item.Attributes("label").Value == "test"
|
88
|
+
// select item;
|
89
|
+
|
90
|
+
// コンパイルエラー
|
91
|
+
// そうだよな複数形(Attributes)だもんな
|
92
|
+
// えっと。。こうかな??
|
93
|
+
var cc = from item in xml.Descendants(ns + "button")
|
94
|
+
from att in item.Attributes("label")
|
95
|
+
where att.Value == "test"
|
96
|
+
select item;
|
97
|
+
Console.WriteLine(c.Count()); // 1
|
98
|
+
|
99
|
+
// ふぅやっとできたっぽいぞー
|
100
|
+
// ほんとにダイジョブかテストをしとこう
|
101
|
+
|
102
|
+
|
103
|
+
// xmlのbuttonの行を増やす・labelの文字を変える等 あってるかのテスト
|
104
|
+
|
105
|
+
|
106
|
+
// よーしだいじょぶそう
|
107
|
+
// で、なにやってたんだっけ?w
|
108
|
+
// あぁそうそう
|
109
|
+
var info = (from item in xml.Descendants(ns + "button")
|
110
|
+
from att in item.Attributes("label")
|
111
|
+
where att.Value == "test"
|
112
|
+
select item).Single(); // Singleは必ず1つだけ該当するという前提ならOK
|
113
|
+
|
114
|
+
info.Attribute("label").Value = "aaaaa";
|
115
|
+
xml.Save(@"Ribbon1.xml");
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
```
|
120
|
+
|
121
|
+
Linqに不慣れなら`foreach`で書いたほうがいいかもしれません。
|
122
|
+
```cs
|
123
|
+
using System.Xml.Linq;
|
124
|
+
|
125
|
+
namespace Questions352772
|
126
|
+
{
|
127
|
+
internal class Program
|
128
|
+
{
|
129
|
+
private static void Main()
|
130
|
+
{
|
131
|
+
var xml = XElement.Load(@"Ribbon1.xml");
|
132
|
+
XNamespace ns = "http://schemas.microsoft.com/office/2009/07/customui";
|
133
|
+
|
134
|
+
foreach (var button in xml.Descendants(ns + "button"))
|
135
|
+
{
|
136
|
+
foreach (var att in button.Attributes("label"))
|
137
|
+
{
|
138
|
+
if (att.Value == "test")
|
139
|
+
{
|
140
|
+
button.Attribute("label").Value = "aaaaa";
|
141
|
+
xml.Save(@"Ribbon1.xml");
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
```
|
150
|
+
|
151
|
+
> OnActionというイベントも動的に追加したいと考えています。
|
152
|
+
|
153
153
|
VSTOについてはなにもわかりません^^;
|