A large portion of the functionality for working with nodes and elements can be obtained through the
XElement class, and the XDocument class should be used only when you absolutely need the capability to
work at the document level and need access to comments, processing instructions, and the declaration.
Basically, a declaration, comments, and processing instructions are not required for LINQ to XML to
work with XML; you need to use the XDocument class only if you need the functionality it provides.
For instance, the following example creates a simple XML document with several elements and an
attribute, as well as a processing instruction and comments.
XDocument doc = new XDocument(
new XProcessingInstruction("xml-stylesheet", "title=??™EmpInfo??™"),
new XComment("some comments"),
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")
)
)
)
new XComment("more comments"),
);
This code produces the following:
Scott Klein
Geek
02/05/2007
M
Notice how simple it is to construct the XML document and place comments and other information
throughout it.
Pages:
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197