Reading and Querying XML Files in C#
The following code snippet reads an XML file and queries its nodes to retrieve information using the DxF.XML library. Let’s examine the steps of the code:
- First, we start by loading an XML file named “Sample.xml” into a MemoryStream. This step allows us to read the XML file in memory.csharpCopy code
using (MemoryStream xmlStream = new MemoryStream(File.ReadAllBytes("Sample.xml")))
- Next, we create an XmlReader to read the XML data. XmlReader is a class used to read XML data.csharpCopy code
using (XmlReader xmlReader = new XmlReader(xmlStream))
- We use the
GetNodeCount
method to get the total number of 'book' nodes and filter them by a specific author if desired.csharpCopy code
int countBook = xmlReader.GetNodeCount("book", "author1");
- Then, we retrieve the values of specific nodes for each book. The
GetNode
method returns the value based on a specific book and node. For example,GetNode("book[" + i + "]", "author")
retrieves the value of the author for the book at indexi
.csharpCopy code
string authorValue = xmlReader.GetNode("book[" + i + "]", "author");
string titleValue = xmlReader.GetNode("book[" + i + "]", "title");
string genreValue = xmlReader.GetNode("book[" + i + "]", "genre");
string priceValue = xmlReader.GetNode("book[" + i + "]", "price");
string publish_dateValue = xmlReader.GetNode("book[" + i + "]", "publish_date");
string descriptionValue = xmlReader.GetNode("book[" + i + "]", "description");
- Finally, we print the details of each book to the console. In this step, we individually print the book details such as the author, title, genre, price, publish date, and description.csharpCopy code
Console.WriteLine("----Book " + i + "--------");
Console.WriteLine("Author: " + authorValue);
Console.WriteLine("Title: " + titleValue);
Console.WriteLine("Genre: " + genreValue);
Console.WriteLine("Price: " + priceValue);
Console.WriteLine("Publish Date: " + publish_dateValue);
Console.WriteLine("Description:");
Console.WriteLine(descriptionValue);
Open Source Code on GitHub:
The example code and additional detailed explanations shared in this article can be found in the ksomaz/DxF.XML GitHub repository. This open-source project demonstrates how to read and query XML files within a broader application. The repository includes more examples, tests, and documentation.
Feel free to visit the GitHub repository to contribute, ask questions, or provide suggestions. You can also customize and enhance the project according to your needs.
Conclusion:
In this article, we have learned how to read and query XML files using the DxF.XML library in C#. DxF.XML is a helpful tool for working with XML data and offers various features that can be utilized in different projects. By following the steps explained in the article, you can access data in XML files, perform queries, and execute operations.
We hope this article assists C# programmers in working with XML files and contributes to their understanding of using the DxF.XML library. Happy coding!