106
Chapter 5: Understanding LINQ to XML
Once the XML document is created, the NodesAfterSelf method of the XElement class is used to return
all the elements after the
element. Those elements are then iterated through and added to
the list box. This example requires a Using statement to System.Xml.
XElement doc = new XElement("Root",
new XElement("Employees",
new XElement("Employee",
new XAttribute("id" "1"),
new XElement("Name", "Scott Klein"),
new XElement("Title", "Geek"),
new XElement("HireDate", "02/05/2007"),
new XElement("Gender", "M")
)
)
);
XElement xele = xtree. Element("Employees").Element("Employee"). Element("Name");
IEnumerable nodes =
from node in xele.NodesAfterSelf()
select node;
foreach (XNode inode in nodes)
listBox1.Items.Add(inode.NodeType == XmlNodeType.Element ?
(inode as XElement).Value : "");
Now you should be able to see how easy and efficient it is to work with XML in LINQ to XML, using the
available classes to create, query, and manipulate XML.
LINQ to XML Programming Concepts
This section explores LINQ to XML programming concepts such as how to load XML, create XML from
scratch, manipulate XML information, and traverse an XML document.
Pages:
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200