XML (eXtensible Markup Language) serves as a versatile data storage and interchange format where developers define their own tags according to specific needs. Unlike HTML with its predefined tags, XML provides complete flexibility in structuring data. While Document Type Definitions (DTD) can enforce validation rules on XML documents, they are optional depending on the use case.
XML Docmuent Structure Requirements
When authoring XML documents, certain rules must be followed to ensure validity:
- A single root element must encapsulate all other elements
- Every opening tag requires a corresponding closing tag
- Attribute values must be enclosed in quotes
Here is an example demonstrating a properly structured XML document:
<students>
<class name="T139">
<student id="1" name="Zhang San" />
<student id="2" name="Li Si" />
<student id="3" name="Wang Wu" />
<student id="4" name="Zhao Liu" />
</class>
<class name="T138">
<student id="1" name="Zhang Yi" />
<student id="2" name="Li Er" />
<student id="3" name="Wang San" />
<student id="4" name="Zhao Si" />
</class>
</students>
To ensure XML files are included in the output directory during debugging, configure the file's Copy to Output Directory property to "Copy if Newer". This seting ensures the file is copied only when directly modified, not when altered programmatically.
XmlDocument Operations
The System.Xml namespace provides comprehensive XML manipulation capabilities. The XmlDocument class serves as the primary interface for loading, navigating, and modifying XML documents, analogous to how HTML operations utilize the DOM.
XmlDocument document = new XmlDocument();
document.Load("path/to/file.xml");
// Access the root element
XmlElement root = document.DocumentElement;
// Traverse child nodes
foreach (XmlNode child in root.ChildNodes)
{
Console.WriteLine(child.Name);
}
// Retrieve attribute values
string className = root["class"].Attributes["name"].Value;
// Create new elements
XmlElement newStudent = document.CreateElement("student");
newStudent.SetAttribute("id", "5");
newStudent.SetAttribute("name", "New Student");
root.AppendChild(newStudent);
// Create attributes programmatically
XmlAttribute newAttr = document.CreateAttribute("grade");
newAttr.Value = "A";
newStudent.Attributes.Append(newAttr);
// Persist changes to file
document.Save("path/to/output.xml");
// Query nodes using XPath expressions
XmlNodeList matches = document.SelectNodes("students/class[@name='T139']/student");
XPath Navigation
XPath expressions enable precise navigation through XML hierarchies. The syntax supports filtering by attribute values:
"students/class[@name='T139']/student" // Selects all student elements within class elements where name equals 'T139'
LINQ to XML
For scenarios requiring query-based XML manipulation, the LINQ to XML API provides a more intuitive approach compared to traditional DOM methods.
Applictaion Configuration Files
XML files commonly serve as configuration sources in .NET applications. The appSettings section stores lightweight application parameters such as display preferences, branding assets, and default credentials—information that would be inefficient to store in a database.
<appSettings>
<add key="fontcolor" value="red"/>
<add key="fontsize" value="12"/>
<add key="theme" value="dark"/>
</appSettings>
Access these settings programmatically using the ConfigurationManager class:
string fontColor = ConfigurationManager.AppSettings["fontcolor"];
string fontSize = ConfigurationManager.AppSettings["fontsize"];
string theme = ConfigurationManager.AppSettings["theme"];
This approach eliminates database queries for frequently accessed configuration data, improving application responsiveness.