{"id":2073,"date":"2011-02-25T12:26:58","date_gmt":"2011-02-25T03:26:58","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=2073"},"modified":"2011-02-25T12:29:54","modified_gmt":"2011-02-25T03:29:54","slug":"%e3%82%82%e3%81%86-linq-to-xml-%e3%81%af%e3%81%84%e3%82%89%e3%81%aa%e3%81%84%e9%9b%bb%e6%92%83%e6%88%a6","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/2073","title":{"rendered":"\u3082\u3046 LINQ to XML \u306f\u3044\u3089\u306a\u3044(\u96fb\u6483\u6226)"},"content":{"rendered":"<p>NUnit \u306e\u8aac\u660e\u7528\u306b\u4f5c\u3063\u3066\u307f\u305f\u306e\u3067\u3059\u304c\u3001\u8aac\u660e\u306b\u3057\u3066\u306f\u30b3\u30fc\u30c9\u304c\u96e3\u3057\u3044\u306e\u3067\u3001\u3053\u3063\u3061\u306b\u306e\u307b\u3046\u306b\u516c\u958b\u3057\u307e\u3059\u3002<br \/>\n\u30b3\u30fc\u30c9\u306b\u95a2\u3057\u3066\u306f\u3001\u305f\u3063\u305f 400 \u884c\u5f31\u306e\u5b9f\u88c5\u3067\u3001\u540c\u3058\u3050\u3089\u3044\u306e\u9577\u3055\u306e NUnit \u306e\u30b3\u30fc\u30c9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u6700\u521d\u306f\u3001\u3042\u307e\u308a\u30c6\u30b9\u30c8\u30d5\u30a1\u30fc\u30b9\u30c8\u3067\u4f5c\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u7121\u99c4\u306a\u30b3\u30fc\u30c9\u304c\u6df7\u3058\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3072\u3068\u307e\u305a\u4f5c\u3063\u305f\u3068\u304d\u306e\u624b\u9806\u3092\u3055\u3089\u3057\u3066\u884c\u304d\u307e\u3059\u306d\u3002\u3082\u3046\u3061\u3087\u3063\u3068\u307e\u3068\u3081\u305f\u3089 codeplex \u306b\u3067\u3082\u516c\u958b\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u57fa\u672c\u30af\u30e9\u30b9\u3092\u3056\u3063\u3068\u8a2d\u8a08\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclass EXDocument {\r\n\tXmlDocument _xmldoc ;\r\n}\r\nclass EXElement {\r\n\tXmlElement _xmlelement ;\r\n}\r\nclass EXElement : List&lt;EXElement&gt;\r\nclass EXAttr {\r\n\tXmlAttribute _xmlattr ;\r\n}\r\nclass EXAttrs : List&lt;EXAttr&gt;\r\n<\/pre>\n<p>\u3053\u308c\u3060\u3051\u3067\u3059\uff57\u3000\u5185\u90e8\u306b XmlDocument\/XmlElement \u3092\u6301\u305f\u305b\u3066\u3001\u305d\u308c\u3092\u53c2\u7167\u3057\u3066 XPath \u98a8\u306b\u52d5\u304b\u3057\u307e\u3059\u3002<br \/>\nC++ \u306e\u6642\u306f\u3001\u3053\u306e\u3042\u305f\u308a\u306e\u5b9f\u88c5\u3082\u8003\u3048\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001C# \u306f\u9762\u5012\u306a\u306e\u3067 XML \u95a2\u4fc2\u306f\u304a\u4efb\u305b\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Test]\r\npublic void TestRoot()\r\n{\r\n    EXDocument doc = new EXDocument();\r\n    doc.LoadXML(@&amp;quot;\r\n&lt;members&gt;\r\n\t&lt;person&gt;masuda&lt;\/person&gt;\r\n&lt;\/members&gt;\r\n&amp;quot;);\r\n    EXElement el = doc \/ &amp;quot;members&amp;quot;;\r\n    Assert.IsNotNull(el);\r\n    Assert.AreEqual(&amp;quot;members&amp;quot;, el.Name);\r\n    Assert.AreEqual(&amp;quot;&amp;quot;, el.Value);\r\n\r\n    EXElements els = doc \/ &amp;quot;members&amp;quot; \/ &amp;quot;person&amp;quot;;\r\n    Assert.IsNotNull(els);\r\n    Assert.AreEqual(1, els.Count);\r\n    el = els&#x5B;0];\r\n    Assert.IsNotNull(el);\r\n    Assert.AreEqual(&amp;quot;person&amp;quot;, el.Name);\r\n    Assert.AreEqual(&amp;quot;masuda&amp;quot;, el.Value);\r\n}\r\n<\/pre>\n<p>\u6700\u521d\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u304c\u3053\u308c\u3067\u3059\u3002<\/p>\n<p>EXDocument::LoadXML \u306f\u3001\u5358\u7d14\u306b XmlDocument::LoadXML \u3092\u547c\u3073\u51fa\u3059\u3060\u3051\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u3042\u305f\u308a\u306f\u3010\u30c6\u30b9\u30c8\u30d5\u30a1\u30fc\u30b9\u30c8\u3011\u3067\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u306e\u56de\u306e\u5834\u5408\u306f UI \u8a2d\u8a08\u3082\u540c\u6642\u306b\u306f\u3044\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\nUI \u8a2d\u8a08\u3068\u3044\u3046\u8a00\u8449\u3092\u4f7f\u3046\u306e\u306f\u3001EXDocument \u3092\u3069\u306e\u3088\u3046\u306b\u6271\u3046\u306e\u304b\uff1f\u3069\u306e\u3088\u3046\u306b\u6271\u3044\u305f\u3044\u306e\u304b\uff1f\u3068\u3044\u3046\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306e\u8a2d\u8a08\u3060\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>XML \u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3093\u3060\u5f8c\u306f\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\t\/\/ \u30eb\u30fc\u30c8\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\r\n    EXElement el = doc \/ &quot;members&quot;;\r\n\r\n\t\/\/ person \u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\r\n    EXElements els = doc \/ &quot;members&quot; \/ &quot;person&quot;;\r\n\r\n<\/pre>\n<p>\u3068\u3044\u3046\u4f7f\u3044\u65b9\u304c\u3057\u305f\u3044\u3001\u3068\u3044\u3046(\u81ea\u5206\u306e)\u8981\u4ef6\u306a\u306e\u3067\u3059\u3002<\/p>\n<p>\u3055\u3066\u3001\u3053\u308c\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u307e\u305a\u306f\u3001\u300cEXDocument \u3068 string \u3067 \/ \u6f14\u7b97\u5b50\u306f\u4f7f\u3063\u3066 EXElement \u306b\u5909\u63db\u3067\u304d\u306a\u3044\u300d\u3068\u3044\u3046\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u304c\u3067\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u8a33\u3067\u3001\u4ef0\u305b\u306e\u901a\u308a EXDocument \u30af\u30e9\u30b9\u306b \/ \u6f14\u7b97\u5b50\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static EXElement operator \/(EXDocument doc, string s)\r\n{\r\n    if (doc._documentElement != null)\r\n    {\r\n        if (doc._documentElement._xmlelement.Name == s)\r\n        {\r\n            return doc._documentElement;\r\n        }\r\n        else\r\n        {\r\n            return EXDocument._emptyElement;\r\n        }\r\n    }\r\n    return null;\r\n}\r\n<\/pre>\n<p>_documentElement \u3092\u53c2\u7167\u3057\u3066\u3044\u308b\u306e\u304c\u5197\u9577\u3067\u3059\u304c\u3001\u30eb\u30fc\u30c8\u8981\u7d20\u306e\u30bf\u30b0\u540d\u3068\u5f15\u6570\u306e\u6587\u5b57\u5217\u3092\u6bd4\u8f03\u3057\u3066\u3001\u30de\u30c3\u30c1\u3057\u305f\u3089\u30eb\u30fc\u30c8\u8981\u7d20\u3092\u8fd4\u3059\u3002\u30de\u30c3\u30c1\u3057\u306a\u304b\u3063\u305f\u3089\u7a7a\u306e\u8981\u7d20\u3092\u8fd4\u3059\u3001\u3063\u3066\u3053\u3068\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p># \u7a7a\u306e\u8981\u7d20\u3092\u8fd4\u3059\u306e\u306f\u3001\u6271\u3044\u304c\u4fbf\u5229\u306b\u306a\u308b null object \u30d1\u30bf\u30fc\u30f3(empty object \u30d1\u30bf\u30fc\u30f3\uff1f\uff09\u3067\u3059\u3002<br \/>\n# null \u30dd\u30a4\u30f3\u30bf\u3092\u8fd4\u3057\u3066\u30a8\u30e9\u30fc\u306b\u3059\u308b\u3088\u308a\u3082\u3001\u4f55\u3082\u3057\u306a\u3044\u3082\u306e\u3092\u3055\u3089\u3063\u3068\u8fd4\u3059\u307b\u3046\u304c\u3001\u547c\u3073\u51fa\u3057\u5074\u306e\u30b3\u30fc\u30c9\u304c\u77ed\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3060\u3051\u306e\u30b3\u30fc\u30c9\u3067\u3001<\/p>\n<p>    EXElement el = doc \/ &#8220;members&#8221;;<\/p>\n<p>\u306e\u90e8\u5206\u306f\u901a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u66f4\u306b\u3001\u3082\u3046\u3072\u3068\u3064\u306e\u8981\u4ef6\u3092\u30af\u30ea\u30a2\u3059\u305f\u3081\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u3092\u898b\u307e\u3059\u3002<br \/>\n\u300cEXElement \u3068 string \u306e \/ \u6f14\u7b97\u5b50\u3067\u3001EXElements \u306b\u5909\u63db\u3067\u304d\u306a\u3044\u300d\u3068\u3044\u3046\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3082\u5148\u306e\u30b3\u30fc\u30c9\u3068\u540c\u3058\u3088\u3046\u306b EXElement \u30af\u30e9\u30b9\u306b \/ \u6f14\u7b97\u5b50\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static EXElements operator \/(EXElement el, string tag)\r\n{\r\n    EXElements els = new EXElements();\r\n    foreach ( XmlNode nd in el._xmlelement.ChildNodes ) \r\n    {\r\n        XmlElement xl = nd as XmlElement;\r\n        if ( xl != null ) {\r\n            if ( xl.Name == tag ) {\r\n                EXElement ne =  el._document.CreateElement();\r\n                ne._xmlelement = xl;\r\n                els.Add(ne);\r\n            }\r\n        }\r\n    }\r\n    return els;\r\n}\r\n<\/pre>\n<p>\u3084\u3084\u3053\u3057\u3044\u305d\u3046\u3067\u3059\u304c\u3001\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u5927\u3057\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n\u5bfe\u5fdc\u3059\u308b XmlElement \u306e\u5b50\u8981\u7d20\u306b\u5bfe\u3057\u3066\u30bf\u30b0\u540d\u306b\u30de\u30c3\u30c1\u3059\u308b\u3082\u306e\u3092\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u3002\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u304c\u3070\u3093\u3070\u3093\u4f5c\u3089\u308c\u307e\u3059\u304c\u3001\u307e\u3041\u3001\u4e2d\u8eab\u306e XmlElement \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30dd\u30a4\u30f3\u30bf\u3068\u3057\u3066\u53c2\u7167\u3059\u308b\u306e\u3067\u3001\u30e1\u30e2\u30ea\u7684\u306b\u306f\u554f\u984c\u306a\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u3053\u3093\u306a\u98a8\u306b\u3059\u308b\u3068\u3001\u3055\u3063\u304f\u308a\u3068\u3001\u4e0b\u8a18\u306e\uff12\u3064\u306e\u30b3\u30fc\u30c9\u304c\u30b3\u30f3\u30d1\u30a4\u30eb\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\t\/\/ \u30eb\u30fc\u30c8\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\r\n    EXElement el = doc \/ &quot;members&quot;;\r\n\r\n\t\/\/ person \u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\r\n    EXElements els = doc \/ &quot;members&quot; \/ &quot;person&quot;;\r\n\r\n<\/pre>\n<p>\u304a\u6b21\u306f\u3001EXElement \u3068\u304b EXElements \u306e\u307e\u307e\u3060\u3068\u4f7f\u3044\u3065\u3089\u3044\u306e\u3067\u3001string \u306b\u76f4\u63a5\u4ee3\u5165\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NUnit \u306e\u8aac\u660e\u7528\u306b\u4f5c\u3063\u3066\u307f\u305f\u306e\u3067\u3059\u304c\u3001\u8aac\u660e\u306b\u3057\u3066\u306f\u30b3\u30fc\u30c9\u304c\u96e3\u3057\u3044\u306e\u3067\u3001\u3053\u3063\u3061\u306b\u306e\u307b\u3046\u306b\u516c\u958b\u3057\u307e\u3059\u3002 \u30b3\u30fc\u30c9\u306b\u95a2\u3057\u3066\u306f\u3001\u305f\u3063\u305f 400 \u884c\u5f31\u306e\u5b9f\u88c5\u3067\u3001\u540c\u3058\u3050\u3089\u3044\u306e\u9577\u3055\u306e NUnit \u306e\u30b3\u30fc\u30c9\u304c\u3042\u308a\u307e\u3059\u3002 \u6700\u521d\u306f\u3001\u3042\u307e\u308a &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/2073\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23,24],"tags":[],"class_list":["post-2073","post","type-post","status-publish","format-standard","hentry","category-csharp","category-xunit"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=2073"}],"version-history":[{"count":4,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2073\/revisions"}],"predecessor-version":[{"id":2077,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2073\/revisions\/2077"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=2073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=2073"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=2073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}