{"id":146,"date":"2017-03-25T15:07:11","date_gmt":"2017-03-25T06:07:11","guid":{"rendered":"http:\/\/shinke1987.net\/?p=146"},"modified":"2017-03-25T15:08:36","modified_gmt":"2017-03-25T06:08:36","slug":"post-146","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=146","title":{"rendered":"C#\u3067XML\u3092\u7c21\u6613\u7684\u306b\u6271\u3046\u30c6\u30b9\u30c8\u3002"},"content":{"rendered":"<h2>\u74b0\u5883<\/h2>\n<p>OS\uff1aWindows7 Pro<br \/>\nVisualStudio\uff1a2013 Professional<br \/>\n.NET Framework\uff1a4.6.1<\/p>\n<h2>\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Text;              \/\/ UTF8Encoding\u30af\u30e9\u30b9\u3092\u4f7f\u3046\u306e\u306b\u5fc5\u8981\u3002\r\nusing System.IO;                \/\/ \u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u66f8\u304d\u3092\u3059\u308b\u306e\u306b\u5fc5\u8981\u3002\r\nusing System.Xml.Linq;          \/\/ XDocument\u7b49\u3092\u4f7f\u3046\u306e\u306b\u5fc5\u8981\u3002\r\nusing System.Windows.Forms;     \/\/ MessageBox\u3067\u5fc5\u8981\u3002\r\n\r\n\/***************************************************************\r\n * \u53c2\u7167\u8a2d\u5b9a\u306b\u3001\r\n * System\r\n * System.Windows.Forms     \uff1aMessageBox\u3067\u5fc5\u8981\u3002\r\n * System.Xml               \uff1aXDocument\u7b49\u3067\u5fc5\u8981\u3002\r\n * System.Xml.Linq          \uff1aXDocument\u7b49\u3067\u5fc5\u8981\u3002\r\n * \u3092\u8ffd\u52a0\u3059\u308b\u3002\r\n * \r\n * \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u3001\r\n * \u300c\u51fa\u529b\u306e\u7a2e\u985e\u300d\u3092\u300c\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u300d\u304b\u3089\r\n * \u300cWindows\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u300d\u3078\u5909\u66f4\u3002\r\n * \r\n * \u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u308b\u30d5\u30a9\u30eb\u30c0\u306b\u3001temp.xml\u304c\u3067\u304d\u308b\u3002\r\n * \r\n **************************************************************\/\r\npublic class TestXML\r\n{\r\n    public static void Main()\r\n    {\r\n        \/\/ XML\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3002\r\n        \/\/ XML\u3092\u8a2d\u5b9a\u3002\r\n        XDocument xml = new XDocument();\r\n        \/\/ XML\u306e1\u884c\u76ee\u306e\u5ba3\u8a00\u90e8\u5206\u3092\u8a2d\u5b9a\u3002standalone=yes\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u3002\r\n        XDeclaration xmlDeclaration = new XDeclaration(&quot;1.0&quot;, &quot;utf-8&quot;, &quot;yes&quot;);\r\n        xml.Declaration = xmlDeclaration;\r\n        XElement xeName = new XElement(&quot;Name&quot;, &quot;\u7530\u4e2d\u592a\u90ce&quot;);\r\n        XElement xeTool = new XElement(&quot;Tool&quot;);\r\n        xeTool.Add(new XElement(&quot;First&quot;, &quot;\u304b\u3070\u3093&quot;));\r\n        xeTool.Add(new XElement(&quot;Second&quot;, &quot;\u306f\u3055\u307f&quot;));\r\n        XElement xePerson = new XElement(&quot;Person&quot;, xeName, xeTool);\r\n        \/\/ XML\u306b\u30ce\u30fc\u30c9\u3092\u8ffd\u52a0\u3002\r\n        xml.Add(xePerson);\r\n        \/\/ \u66f8\u304d\u8fbc\u3080\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u3002\r\n        StreamWriter sw = new StreamWriter(@&quot;.\/temp.xml&quot;, false, new UTF8Encoding(true));\r\n        \/\/ \u66f8\u304d\u8fbc\u3080\u3002\r\n        xml.Save(sw);\r\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3002\r\n        sw.Close();\r\n        MessageBox.Show(&quot;XML\u66f8\u304d\u8fbc\u307f\u5b8c\u4e86\uff01&quot;);\r\n\r\n        \/\/ XML\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3002\r\n        \/\/ \u8aad\u307f\u8fbc\u3080\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u3002\r\n        StreamReader sr = new StreamReader(@&quot;.\/temp.xml&quot;, new UTF8Encoding(true));\r\n        \/\/ \u30eb\u30fc\u30c8\u30ce\u30fc\u30c9(Person)\u306b\u3042\u308b\u8981\u7d20\u3092\u5168\u3066\u8aad\u307f\u8fbc\u3080\u3002\r\n        XElement xElm = XElement.Load(sr);\r\n        \/\/ \u8aad\u307f\u8fbc\u3080\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3002\r\n        sr.Close();\r\n        \/\/ \u8aad\u307f\u8fbc\u3093\u3060\u3082\u306e\u3092\u8868\u793a\u3002\r\n        MessageBox.Show(&quot;\u540d\u524d\uff1a&quot; + xElm.Element(&quot;Name&quot;).Value);\r\n        MessageBox.Show(&quot;Tool First\uff1a&quot; + xElm.Element(&quot;Tool&quot;).Element(&quot;First&quot;).Value);\r\n        MessageBox.Show(&quot;Tool Second\uff1a&quot; + xElm.Element(&quot;Tool&quot;).Element(&quot;Second&quot;).Value);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u74b0\u5883 OS\uff1aWindows7 Pro VisualStudio\uff1a2013 Professional .NET Framework\uff1a4.6.1 \u30b3\u30fc\u30c9 using System; using System.Text; \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[36,8],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-c-sharp","tag-c-sharp","tag-xml"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=146"}],"version-history":[{"count":5,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}