Elements("Employee").Attributes()
select emp;
foreach (XAttribute att in atts)
listBox1.Items.Add(att);
Running this code results in the following:
id="1"
EyeColor="Blue"
Notice that you get the attribute key/value pair. To get just the value, use the Value() property of the
XAttribute class:
foreach (XAttribute att in atts)
listBox1.Items.Add(att.Value.ToString());
And here??™s the result:
1
Blue
In the preceding examples, the XML tree consisted of a single employee. Suppose that the XML tree
consists of multiple employees. The following XML tree contains two employees, and the code then
applies the First() property to get the attributes of the first employee.
XElement employee = new XElement("Root",
new XElement("Employee",
new XAttribute("id", "1"),
new XAttribute("EyeColor", "Green"),
new XElement("Name", "John"),
new XElement("Address", "444 Main St."),
new XElement("City", "Seattle"),
new XElement("State", "WA")
),
new XElement("Employee",
new XAttribute("id", "2"),
new XAttribute("EyeColor", "Blue"),
new XElement("Name", "Scott"),
new XElement("Address", "555 Main St.
Pages:
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216