http://www.atmarkit.co.jp/ait/articles/1704/19/news021.html
C#でXMLをシリアライズする方法を調べていてこちらのサイトを閲覧したのですが、
シリアライズ先のファイルを指定している箇所の「@」はどのような意味があるのでしょうか?
初歩的な質問で申し訳ありませんがご教授頂けますと幸いです。
該当コード
C#
1const string xmlFile = @".\Sample.xml";
全文
C#
1using System.IO; 2using System.Text; 3using System.Xml.Serialization; 4using static System.Console; 5 6// シリアライズ対象のクラス 7public class Sample 8{ 9 public int Id { get; set; } 10 public string Text { get; set; } 11} 12 13class Program 14{ 15 static void Main(string[] args) 16 { 17 // シリアライズ先のファイル 18 const string xmlFile = @".\Sample.xml"; 19 // シリアライズするオブジェクト 20 var obj = new Sample { Id = 7, Text = "@IT" }; // (1) 21 22 // シリアライズする 23 var xmlSerializer1 = new XmlSerializer(typeof(Sample)); 24 using (var streamWriter = new StreamWriter(xmlFile, false, Encoding.UTF8)) 25 { 26 xmlSerializer1.Serialize(streamWriter, obj); 27 streamWriter.Flush(); 28 } 29 // 書き出されたファイルの内容(一部に改行を入れている): 30 // <?xml version="1.0" encoding="utf-8"?> 31 // <Sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 32 // xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 33 // <Id>7</Id> 34 // <Text>@IT</Text> 35 // </Sample> 36 37 // デシリアライズする 38 var xmlSerializer2 = new XmlSerializer(typeof(Sample)); 39 Sample result; 40 var xmlSettings = new System.Xml.XmlReaderSettings() 41 { 42 CheckCharacters = false, // (2) 43 }; 44 using (var streamReader = new StreamReader(xmlFile, Encoding.UTF8)) 45 using (var xmlReader 46 = System.Xml.XmlReader.Create(streamReader, xmlSettings)) 47 { 48 result = (Sample)xmlSerializer2.Deserialize(xmlReader); // (3) 49 } 50 WriteLine($"{result.Id}, {result.Text}"); 51 // 出力:7, @IT 52 53#if DEBUG 54 ReadKey(); 55#endif 56 } 57}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/12 12:12