You can add attributes using an XML
construction like the following:
XElement employee = new XElement("Root",
new XElement("Employee",
new XAttribute("id", "1"),
new XAttribute("EyeColor", "Blue"),
new XElement("Name", "Scott"),
new XElement("Address", "555 Main St."),
new XElement("City", "Wellington"),
new XElement("State", "FL")
)
);
You can also add an attribute as follows:
XAttribute dept = employee.Element("Employee").Attribute("EyeColor");
Retrieving
Retrieving attributes is also easy. It involves using the Attributes(XName) method of the XElement
class. For example, the following code defines an XML tree with several attributes defined on the
Employee node. The Attributes() method of the XElement class is then used to retrieve those attributes.
XElement employee = new XElement("Root",
new XElement("Employee",
new XAttribute("id", "1"),
new XAttribute("EyeColor", "Blue"),
new XElement("Name", "Scott"),
new XElement("Address", "555 Main St."),
117
Part II: LINQ to XML
new XElement("City", "Wellington"),
new XElement("State", "FL")
)
);
IEnumerable
atts =
from emp in employee.
Pages:
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215