XML ファイルを読み込む c# 実装例をいくつか紹介してみたいと思います。
XmlDocument によるプログラム例を記載します。
本方式の特徴は下記の通りだそうです。
コンパイラ : | Visual Studio 2022 pro., | Version 17.9.0 |
言語: | C# | |
フレームワーク: | .NET 8.0 | |
OS : | Windows11 home, | 22H2 |
XML ファイルサンプル ["sample.xml"]
<?xml version="1.0" encoding="utf-8" ?> <repositories> <!-- Application 1 --> <repository name="Application 1"> <host_pc>HOST1</host_pc> <host_pc_ip>192.168.0.11</host_pc_ip> <branches> <branch name="main"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="develop"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="feature/add_new_feature"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="bug/memory_leak_happen"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="release/release_candidate_2024-02"> <build_options> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> </build_options> </branch> </branches> </repository> <!-- Application 2 --> <repository name="Application 2"> <host_pc>HOST2</host_pc> <host_pc_ip>192.168.0.12</host_pc_ip> <branches> <branch name="main"> <build_options> <build_option name="-lm -Wall -g3 -O0 -coverage"> <build_directory>debug</build_directory> </build_option> <build_option name="-lm -Wall -O3"> <build_directory>release</build_directory> </build_option> </build_options> </branch> <branch name="develop"> <build_options> <build_option name="-lm -Wall -g3 -O0 -coverage"> <build_directory>debug</build_directory> </build_option> <build_option name="-lm -Wall -O3"> <build_directory>release</build_directory> </build_option> </build_options> </branch> </branches> </repository> </repositories>
["Program.cs"]
// // XmlDocument による実装 // using System.Xml; namespace ReadXmlFile { internal class Program { static void Main(string[] args) { var xmlPath = @"sample.xml"; var xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlNode? root = xmlDoc.DocumentElement; if (root != null) { { // リポジトリ一覧 取得 Console.WriteLine("=== <repository> ==="); foreach (XmlNode childNode in root.ChildNodes) { if (childNode.NodeType == XmlNodeType.Element && childNode.LocalName == "repository") { Console.WriteLine(childNode?.Attributes?["name"]?.Value); } } Console.WriteLine(); } { // ブランチ一覧 取得 Console.WriteLine("=== [Application 1].<branches>.<branch> ==="); XmlNode? branches = root.SelectSingleNode("descendant::repository[@name='Application 1']/branches"); if (branches != null) { foreach (XmlNode branch in branches) { Console.WriteLine(branch.Attributes?["name"]?.Value); } } Console.WriteLine(); } { // ビルドオプション一覧 取得 Console.WriteLine("=== [Application 2].[main].<build_option> ==="); XmlNode? build_options = root.SelectSingleNode("descendant::repository[@name='Application 2']/branches/branch[@name='main']/build_options"); if (build_options != null) { foreach (XmlNode build_option in build_options.ChildNodes) { if (build_option.NodeType == XmlNodeType.Element) { Console.WriteLine(build_option.Attributes?["name"]?.Value); } } } Console.WriteLine(); } } Console.Write("Push Enter key ! "); Console.ReadLine(); } } }
実行結果
=== <repository> === Application 1 Application 2 === [Application 1].<branches>.<branch> === main develop feature/add_new_feature bug/memory_leak_happen release/release_candidate_2024-02 === [Application 2].[main].<build_option> === -lm -Wall -g3 -O0 -coverage -lm -Wall -O3 Push Enter key !
XDocument と LINQ によるプログラム例を記載します。
本方式の特徴は下記の通りだそうです。
コンパイラ : | Visual Studio 2022 pro., | Version 17.9.0 |
言語: | C# | |
フレームワーク: | .NET 8.0 | |
OS : | Windows11 home, | 22H2 |
XML ファイルサンプル ["sample.xml"]
<?xml version="1.0" encoding="utf-8" ?> <repositories> <!-- Application 1 --> <repository name="Application 1"> <host_pc>HOST1</host_pc> <host_pc_ip>192.168.0.11</host_pc_ip> <branches> <branch name="main"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="develop"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="feature/add_new_feature"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="bug/memory_leak_happen"> <build_options> <build_option name="-r 1gb -f 512mb"> <build_directory>app_1g_512m</build_directory> </build_option> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> <build_option name="-r 4gb -f 512mb"> <build_directory>app_4g_512m</build_directory> </build_option> </build_options> </branch> <branch name="release/release_candidate_2024-02"> <build_options> <build_option name="-r 2gb -f 512mb"> <build_directory>app_2g_512m</build_directory> </build_option> </build_options> </branch> </branches> </repository> <!-- Application 2 --> <repository name="Application 2"> <host_pc>HOST2</host_pc> <host_pc_ip>192.168.0.12</host_pc_ip> <branches> <branch name="main"> <build_options> <build_option name="-lm -Wall -g3 -O0 -coverage"> <build_directory>debug</build_directory> </build_option> <build_option name="-lm -Wall -O3"> <build_directory>release</build_directory> </build_option> </build_options> </branch> <branch name="develop"> <build_options> <build_option name="-lm -Wall -g3 -O0 -coverage"> <build_directory>debug</build_directory> </build_option> <build_option name="-lm -Wall -O3"> <build_directory>release</build_directory> </build_option> </build_options> </branch> </branches> </repository> </repositories>
["Program.cs"]
// // XDocument による実装 // using System.Xml.Linq; namespace ReadXmlFile { internal class Program { static void Main(string[] args) { var xmlPath = @"sample.xml"; var xmlDoc = XDocument.Load(xmlPath); { // リポジトリ一覧 取得 Console.WriteLine("=== <repositories>.<repository> ==="); var repositories = xmlDoc.Descendants("repository"); foreach (var repository in repositories) { Console.WriteLine(repository.Attribute("name")?.Value); } Console.WriteLine(); } { // ブランチ一覧 取得 Console.WriteLine("=== [Application 1].<branches>.<branch> ==="); var branches = xmlDoc.Descendants("repository") .Where(q => q.Attribute("name")?.Value == "Application 1") .Elements("branches") .Elements("branch"); foreach (var branch in branches) { Console.WriteLine(branch.Attribute("name")?.Value); } Console.WriteLine(); } { // ビルドオプション一覧 取得 Console.WriteLine("=== [Application 2].[main].<build_options>.<build_option> ==="); var build_options = xmlDoc.Descendants("repository") .Where(q => q.Attribute("name")?.Value == "Application 2") .Elements("branches") .Elements("branch") .Where(q => q.Attribute("name")?.Value == "main") .Elements("build_options") .Elements("build_option"); foreach (var build_option in build_options) { Console.WriteLine(build_option.Attribute("name")?.Value); } Console.WriteLine(); } Console.Write("Push Enter key ! "); Console.ReadLine(); } } }
実行結果
=== <repository> === Application 1 Application 2 === [Application 1].<branches>.<branch> === main develop feature/add_new_feature bug/memory_leak_happen release/release_candidate_2024-02 === [Application 2].[main].<build_option> === -lm -Wall -g3 -O0 -coverage -lm -Wall -O3 Push Enter key !
本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。
2024-02-14 | - | 新規作成 |