📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

23行目: 23行目:
<br>
<br>
C#におけるXMLファイルの扱いは、.NET Frameworkの進化とともに改善されており、より効率的で柔軟になってきている。<br>
C#におけるXMLファイルの扱いは、.NET Frameworkの進化とともに改善されており、より効率的で柔軟になってきている。<br>
<br><br>
== XMLファイルの作成 ==
以下の例では、非同期処理とストリーミング処理を使用して、XmlWriterクラスで指定されたXML構造を持つXMLファイルを生成している。<br>
<br>
<syntaxhighlight lang="xml">
<!-- 作成するXMLファイルの構造 -->
<Earthquake>
  <OriginTime>2024-08-23T21:00:00+09:00</OriginTime>
  <ArrivalTime>2024-08-23T21:01:00+09:00</ArrivalTime>
  <Hypocenter>
    <Area>
      <Name>茨城県南部</Name>
      <Code type="震央地名">301</Code>
    </Area>
  </Hypocenter>
  <jmx_eb:Magnitude type="Mj" description="M3.8">3.8</jmx_eb:Magnitude>
</Earthquake>
<Observation>
  <Pref><Name>茨城県</Name><Code>08</Code><MaxInt>2</MaxInt>
    <Area><Name>茨城県北部</Name><Code>300</Code><MaxInt>2</MaxInt>
      <City><Name>小美玉市</Name><Code>0823600</Code><MaxInt>2</MaxInt>
        <IntensityStation><Name>小美玉市小川*</Name><Code>0823633</Code><Int>2</Int></IntensityStation>
        <IntensityStation><Name>小美玉市上玉里*</Name><Code>0823635</Code><Int>2</Int></IntensityStation>
      </City>
      <City><Name>水戸市</Name><Code>0820100</Code><MaxInt>1</MaxInt>
        <IntensityStation><Name>水戸市千波町*</Name><Code>0820121</Code><Int>1</Int></IntensityStation>
      </City>
    </Area>
  </Pref>
</Observation>
</syntaxhighlight>
<br>
<syntaxhighlight lang="c#">
using System;
using System.Xml;
using System.Threading.Tasks;
class Program
{
    static async Task Main(string[] args)
    {
      string xmlFilePath = "sample.xml";
      try
      {
          await CreateEarthquakeXmlAsync(xmlFilePath);
      }
      catch (Exception ex)
      {
          Console.WriteLine($"エラーが発生: {ex.Message}");
      }
    }
    static async Task CreateEarthquakeXmlAsync(string filePath)
    {
      var settings = new XmlWriterSettings
      {
          Async = true,
          Indent = true
      };
      using (var writer = XmlWriter.Create(filePath, settings))
      {
          await writer.WriteStartDocumentAsync();
          // ルート要素は記述せずに直接コンテンツを記述する
          await WriteEarthquakeElementAsync(writer);
          await WriteObservationElementAsync(writer);
          await writer.WriteEndDocumentAsync();
      }
    }
    static async Task WriteEarthquakeElementAsync(XmlWriter writer)
    {
      await writer.WriteStartElementAsync(null, "Earthquake", null);
      await writer.WriteElementStringAsync(null, "OriginTime", null, "2024-08-23T21:00:00+09:00");
      await writer.WriteElementStringAsync(null, "ArrivalTime", null, "2024-08-23T21:01:00+09:00");
      await writer.WriteStartElementAsync(null, "Hypocenter", null);
      await writer.WriteStartElementAsync(null, "Area", null);
      await writer.WriteElementStringAsync(null, "Name", null, "茨城県南部");
       
      await writer.WriteStartElementAsync(null, "Code", null);
      await writer.WriteAttributeStringAsync(null, "type", null, "震央地名");
      await writer.WriteStringAsync("301");
      await writer.WriteEndElementAsync();
      await writer.WriteEndElementAsync();  // Area
      await writer.WriteEndElementAsync();  // Hypocenter
      await writer.WriteStartElementAsync("jmx_eb", "Magnitude", null);
      await writer.WriteAttributeStringAsync(null, "type", null, "Mj");
      await writer.WriteAttributeStringAsync(null, "description", null, "M3.8");
      await writer.WriteStringAsync("3.8");
      await writer.WriteEndElementAsync();
      await writer.WriteEndElementAsync();  // Earthquake
    }
    static async Task WriteObservationElementAsync(XmlWriter writer)
    {
      await writer.WriteStartElementAsync(null, "Observation", null);
      await writer.WriteStartElementAsync(null, "Pref", null);
      await writer.WriteElementStringAsync(null, "Name", null, "茨城県");
      await writer.WriteElementStringAsync(null, "Code", null, "08");
      await writer.WriteElementStringAsync(null, "MaxInt", null, "2");
      await writer.WriteStartElementAsync(null, "Area", null);
      await writer.WriteElementStringAsync(null, "Name", null, "茨城県北部");
      await writer.WriteElementStringAsync(null, "Code", null, "300");
      await writer.WriteElementStringAsync(null, "MaxInt", null, "2");
      // 小美玉市
      await writer.WriteStartElementAsync(null, "City", null);
      await writer.WriteElementStringAsync(null, "Name", null, "小美玉市");
      await writer.WriteElementStringAsync(null, "Code", null, "0823600");
      await writer.WriteElementStringAsync(null, "MaxInt", null, "2");
      await WriteIntensityStationAsync(writer, "小美玉市小川*", "0823633", "2");
      await WriteIntensityStationAsync(writer, "小美玉市上玉里*", "0823635", "2");
      await writer.WriteEndElementAsync();  // City
      // 水戸市
      await writer.WriteStartElementAsync(null, "City", null);
      await writer.WriteElementStringAsync(null, "Name", null, "水戸市");
      await writer.WriteElementStringAsync(null, "Code", null, "0820100");
      await writer.WriteElementStringAsync(null, "MaxInt", null, "1");
      await WriteIntensityStationAsync(writer, "水戸市千波町*", "0820121", "1");
      await writer.WriteEndElementAsync();  // City
      await writer.WriteEndElementAsync();  // Area
      await writer.WriteEndElementAsync();  // Pref
      await writer.WriteEndElementAsync();  // Observation
    }
    static async Task WriteIntensityStationAsync(XmlWriter writer, string name, string code, string intensity)
    {
      await writer.WriteStartElementAsync(null, "IntensityStation", null);
      await writer.WriteElementStringAsync(null, "Name", null, name);
      await writer.WriteElementStringAsync(null, "Code", null, code);
      await writer.WriteElementStringAsync(null, "Int", null, intensity);
      await writer.WriteEndElementAsync();
    }
}
</syntaxhighlight>
<br><br>
<br><br>


== 要素の取得 ==
== 要素の取得 ==
以下の例では、XMLファイルを読み込み、以下に示す要素を読み込んでいる。<br>
以下の例では、非同期処理を使用してXMLファイルを読み込み、以下に示す要素を読み込んでいる。<br>
* <Hypocenter>  ->  <Area>  ->  <Name>の値
* <Hypocenter>  ->  <Area>  ->  <Name>の値
* <Hypocenter>  ->  <Area>  ->  <nowiki><Code></nowiki>のtype属性の値
* <Hypocenter>  ->  <Area>  ->  <nowiki><Code></nowiki>のtype属性の値