13,009
回編集
(→要素の変更) |
(→要素の変更) |
||
549行目: | 549行目: | ||
</Pref> | </Pref> | ||
</Observation> | </Observation> | ||
</syntaxhighlight> | |||
<br><br> | |||
== LINQ to XMLの使用 == | |||
==== 要素の取得 ==== | |||
以下の例では、非同期処理を使用してXMLファイルを読み込み、以下に示す要素を読み込んでいる。<br> | |||
<br> | |||
* <Hypocenter> -> <Area> -> <Name>の値 | |||
* <Hypocenter> -> <Area> -> <Code>のtype属性の値 | |||
* 全ての<Observation> -> <IntensityStation> -> <Name>の値 | |||
<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.Collections.Generic; | |||
using System.Threading.Tasks; | |||
class Program | |||
{ | |||
static async Task Main(string[] args) | |||
{ | |||
try | |||
{ | |||
// XMLファイルのパスを指定 | |||
string xmlFilePath = "sample.xml"; | |||
// XMLファイルを非同期で読み込み、XDocumentオブジェクトを生成 | |||
XDocument doc = await Task.Run(() => XDocument.Load(xmlFilePath)); | |||
// <Hypocenter> -> <Area> -> <Name>の値を取得 | |||
string areaName = doc.Descendants("Hypocenter") | |||
.Elements("Area") | |||
.Elements("Name") | |||
.FirstOrDefault()?.Value; | |||
Console.WriteLine($"震源地域名: {areaName}"); | |||
// <Hypocenter> -> <Area> -> <Code>のtype属性の値を取得 | |||
string codeType = doc.Descendants("Hypocenter") | |||
.Elements("Area") | |||
.Elements("Code") | |||
.Attributes("type") | |||
.FirstOrDefault()?.Value; | |||
Console.WriteLine($"コードタイプ: {codeType}"); | |||
// 全ての<Observation> -> <IntensityStation> -> <Name>の値を取得 | |||
var intensityStationNames = doc.Descendants("Observation") | |||
.Descendants("IntensityStation") | |||
.Elements("Name") | |||
.Select(e => e.Value) | |||
.ToList(); | |||
Console.WriteLine("観測点名:"); | |||
foreach (var name in intensityStationNames) | |||
{ | |||
Console.WriteLine($"- {name}"); | |||
} | |||
// ストリーミング処理を使用して大きなXMLファイルを効率的に処理する例 | |||
await ProcessLargeXmlFileAsync(xmlFilePath); | |||
} | |||
catch (FileNotFoundException) | |||
{ | |||
Console.WriteLine("エラー: 指定されたXMLファイルが存在しない"); | |||
} | |||
catch (Exception ex) | |||
{ | |||
Console.WriteLine($"エラー: XMLの処理中に問題が発生 詳細: {ex.Message}"); | |||
} | |||
} | |||
// 大きなXMLファイルを効率的にストリーミング処理する | |||
static async Task ProcessLargeXmlFileAsync(string filePath) | |||
{ | |||
Console.WriteLine("ストリーミング処理による大規模XMLファイルの処理:"); | |||
using (var reader = new StreamReader(filePath)) | |||
{ | |||
using (var xmlReader = System.Xml.XmlReader.Create(reader)) | |||
{ | |||
while (await xmlReader.ReadAsync()) | |||
{ | |||
if (xmlReader.NodeType == System.Xml.XmlNodeType.Element) | |||
{ | |||
if (xmlReader.Name == "IntensityStation") | |||
{ | |||
// IntensityStationの子要素を処理 | |||
var element = await XElement.ReadFromAsync(xmlReader); | |||
var name = element.Element("Name")?.Value; | |||
var code = element.Element("Code")?.Value; | |||
var intensity = element.Element("Int")?.Value; | |||
Console.WriteLine($"観測点: {name}, コード: {code}, 震度: {intensity}"); | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> |