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

 
(同じ利用者による、間の1版が非表示)
610行目: 610行目:
   
   
           // ファイルに非同期で保存
           // ファイルに非同期で保存
           await SaveXmlToFileAsync(doc, "earthquake_data.xml");
           await SaveXmlToFileAsync(doc, "sample.xml");
       }
       }
       catch (Exception ex)
       catch (Exception ex)
697行目: 697行目:
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
==== 要素の取得 ====
==== 要素の取得 ====
以下の例では、LINQ to XMLおよび非同期処理を使用してXMLファイルを読み込み、以下に示す要素を読み込んでいる。<br>
以下の例では、LINQ to XMLおよび非同期処理を使用してXMLファイルを読み込み、以下に示す要素を読み込んでいる。<br>
834行目: 835行目:
     }
     }
  }
  }
</syntaxhighlight>
<br>
==== 要素の変更 ====
以下の例では、LINQ to XMLを使用して、以下に示すXMLファイルの一部の値を変更している。<br>
<br>
* ModifyXmlFileAsync メソッド:
*: FileStreamクラスを使用して、XMLファイルを非同期で読み込む。
*: LINQ to XMLを使用して、指定された要素と属性を検索して、値を変更する。
*: 変更後のXMLドキュメントを非同期で保存する。
*: <br>
* 変更操作
*: <Hypocenter> -> <Area> -> <Name>の値を"hoge"に変更する。
*: <Hypocenter> -> <Area> -> <nowiki><Code></nowiki>のtype属性の値を"99"に変更する。
*: 全ての<Observation> -> <IntensityStation> -> <Name>の値を"piyo"に変更する。
*: <br>
* 名前空間の処理
*: XMLドキュメントのデフォルト名前空間を取得して、要素の検索に使用する。
* 非同期処理
*: ファイルの読み込みと保存に非同期メソッドを使用する。
* ストリーミング処理
*: FileStreamクラスを使用して、ファイルの読み込みと保存をストリーミング方式で行う。
<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.IO;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;
class Program
{
    static async Task Main(string[] args)
    {
      try
      {
          // XMLファイルのパス
          string filePath = "sample.xml";
          // XMLファイルを非同期で読み込み、修正し、保存する
          await ModifyXmlFileAsync(filePath);
      }
      catch (Exception ex)
      {
          Console.WriteLine($"エラーが発生: {ex.Message}");
      }
    }
    static async Task ModifyXmlFileAsync(string filePath)
    {
      // XMLファイルを非同期で読み込む
      XDocument doc;
      using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
      {
          doc = await XDocument.LoadAsync(fs, LoadOptions.None, default);
      }
      // XMLの名前空間を取得
      XNamespace ns = doc.Root.GetDefaultNamespace();
      // <Hypocenter> -> <Area> -> <Name>の値を変更
      var hypocenterAreaName = doc.Descendants(ns + "Hypocenter")
                                  .Elements(ns + "Area")
                                  .Elements(ns + "Name")
                                  .FirstOrDefault();
      if (hypocenterAreaName != null)
      {
          hypocenterAreaName.Value = "hoge";
      }
      // <Hypocenter> -> <Area> -> <Code>のtype属性の値を変更
      var hypocenterAreaCode = doc.Descendants(ns + "Hypocenter")
                                  .Elements(ns + "Area")
                                  .Elements(ns + "Code")
                                  .FirstOrDefault();
      if (hypocenterAreaCode != null)
      {
          hypocenterAreaCode.Attribute("type").Value = "99";
      }
      // 全ての<Observation> -> <IntensityStation> -> <Name>の値を変更
      var intensityStationNames = doc.Descendants(ns + "Observation")
                                      .Descendants(ns + "IntensityStation")
                                      .Elements(ns + "Name");
      foreach (var name in intensityStationNames)
      {
          name.Value = "piyo";
      }
      // 変更したXMLを非同期で保存
      using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
      {
          await doc.SaveAsync(fs, SaveOptions.None, default);
      }
    }
}
</syntaxhighlight>
<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>hoge</Name>
      <Code type="99">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>piyo</Name><Code>0823633</Code><Int>2</Int></IntensityStation>
        <IntensityStation><Name>piyo</Name><Code>0823635</Code><Int>2</Int></IntensityStation>
      </City>
      <City><Name>水戸市</Name><Code>0820100</Code><MaxInt>1</MaxInt>
        <IntensityStation><Name>piyo</Name><Code>0820121</Code><Int>1</Int></IntensityStation>
      </City>
    </Area>
  </Pref>
</Observation>
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>