Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
The System.Xml.XmlDocument
class in .NET is a powerful tool for working with XML documents. It is part of the System.Xml
namespace and provides a DOM (Document Object Model) representation of an XML document. This allows for easy navigation, manipulation, and querying of XML data. For Windows developers, especially those working with .NET applications, understanding how to use XmlDocument
is crucial for tasks such as configuration management, data interchange, and more.
In this article, we will explore how to create, load, modify, and save XML documents using System.Xml.XmlDocument
in a Windows environment. We will provide practical examples using C# and demonstrate how to run these examples via the Command Prompt (CMD) and PowerShell.
Examples:
Creating an XML Document:
Here is a simple example of how to create an XML document using XmlDocument
in C#.
using System;
using System.Xml;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
// Create the XML declaration
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
// Create the root element
XmlElement rootNode = doc.CreateElement("Books");
doc.AppendChild(rootNode);
// Create a new book element
XmlElement bookElement = doc.CreateElement("Book");
bookElement.SetAttribute("ISBN", "123456789");
// Create and append the title element
XmlElement titleElement = doc.CreateElement("Title");
titleElement.InnerText = "Learning C#";
bookElement.AppendChild(titleElement);
// Append the book element to the root
rootNode.AppendChild(bookElement);
// Save the document to a file
doc.Save("books.xml");
Console.WriteLine("XML Document created and saved as books.xml");
}
}
To compile and run this example via CMD:
csc Program.cs
Program.exe
To run it via PowerShell:
Add-Type -TypeDefinition (Get-Content -Path "Program.cs" -Raw)
Loading and Modifying an XML Document:
This example demonstrates how to load an existing XML document and modify its contents.
using System;
using System.Xml;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
// Select the book element
XmlNode bookNode = doc.SelectSingleNode("/Books/Book[@ISBN='123456789']");
if (bookNode != null)
{
// Modify the title
XmlNode titleNode = bookNode.SelectSingleNode("Title");
if (titleNode != null)
{
titleNode.InnerText = "Mastering C#";
}
// Save the changes
doc.Save("books.xml");
Console.WriteLine("XML Document modified and saved as books.xml");
}
else
{
Console.WriteLine("Book not found.");
}
}
}
To compile and run this example via CMD:
csc Program.cs
Program.exe
To run it via PowerShell:
Add-Type -TypeDefinition (Get-Content -Path "Program.cs" -Raw)
Querying XML Data:
This example shows how to query XML data using XPath.
using System;
using System.Xml;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
// Use XPath to select the title of the book with a specific ISBN
XmlNode titleNode = doc.SelectSingleNode("/Books/Book[@ISBN='123456789']/Title");
if (titleNode != null)
{
Console.WriteLine("" + titleNode.InnerText);
}
else
{
Console.WriteLine("Book not found.");
}
}
}
To compile and run this example via CMD:
csc Program.cs
Program.exe
To run it via PowerShell:
Add-Type -TypeDefinition (Get-Content -Path "Program.cs" -Raw)