回答編集履歴

1

まあ考えがあさかったんですがね

2019/01/11 01:43

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -63,3 +63,77 @@
63
63
  }
64
64
 
65
65
  ```
66
+
67
+
68
+
69
+ ---
70
+
71
+ namespaceの問題ならLocalName見るだけでもできそう。
72
+
73
+ というか、xmlそのものにそれほど詳しいのでないなら(xmlns属性がサンプルで削っていいものだと思うくらいなら)vsで変数参照してrootがnullでなかったら、Descendantsに引数わたさずに全部foreachして各XElementのプロパティをひととおり見るとか、それで分かんないプロパティはMicrosoftDocs見るとか、Console.WriteLineしてみるとかくらいのことは試しておいたほうがよいと思いますよ。
74
+
75
+
76
+
77
+ ```csharp
78
+
79
+ using System;
80
+
81
+ using System.Collections.Generic;
82
+
83
+ using System.Linq;
84
+
85
+ using System.Xml.Linq;
86
+
87
+
88
+
89
+ public class Program
90
+
91
+ {
92
+
93
+ public static void Main()
94
+
95
+ {
96
+
97
+ string str =
98
+
99
+ @"<?xml version=""1.0""?>
100
+
101
+ <Root InterfaceName=""IF"" InterfaceVersion=""1.0"" xmlns=""http://addr/path1/path2"">
102
+
103
+ <Child>
104
+
105
+ <GrandChild1>
106
+
107
+ <Elem1>foo1</Elem1>
108
+
109
+ <Elem2>bar1</Elem2>
110
+
111
+ </GrandChild1>
112
+
113
+ <GrandChild2>
114
+
115
+ <Elem3>foo2</Elem3>
116
+
117
+ <Elem4>bar2</Elem4>
118
+
119
+ </GrandChild2>
120
+
121
+ </Child>
122
+
123
+ </Root>";
124
+
125
+ var root = XElement.Parse(str);
126
+
127
+ var elem1 = from p in root.Descendants()
128
+
129
+ where p.Name.LocalName == "Elem1"
130
+
131
+ select p;
132
+
133
+ Console.WriteLine(string.Join(",", elem1));
134
+
135
+ }
136
+
137
+ }
138
+
139
+ ```