﻿
{"id":759,"date":"2012-05-28T15:57:54","date_gmt":"2012-05-28T15:57:54","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=759"},"modified":"2012-05-28T16:10:24","modified_gmt":"2012-05-28T16:10:24","slug":"c-sharp-ile-xml-dosyasi-olusturmak","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/c-sharp-ile-xml-dosyasi-olusturmak\/","title":{"rendered":"C Sharp \u0130le XML Dosyas\u0131 Olu\u015fturmak"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<pre>Bu yaz\u0131mda C# kodlar\u0131yla XML dosyas\u0131 olu\u015fturmay\u0131 g\u00f6rece\u011fiz.<\/pre>\n<p><!--more--><br \/>\nA\u015fa\u011f\u0131daki \u00f6rnekte veritaban\u0131 olarak Northwind kullan\u0131yorum.<br \/>\nPersoneller tablosunun XML dosyas\u0131n\u0131 kodlarla olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            SqlConnection baglanti = new SqlConnection(&quot;Server=GEN\u00c7AY-GEN\u00c7AY;Database=Northwind;Trusted_Connection=True;&quot;);\r\n            SqlCommand cmd = new SqlCommand(&quot;Select * from Personeller&quot;,baglanti);\r\n            baglanti.Open();\r\n            SqlDataReader dr = cmd.ExecuteReader();\r\n            XmlTextWriter XMLDosyasi = new XmlTextWriter(&quot;Personeller.xml&quot;,Encoding.GetEncoding(1254));\r\n            \/\/XmlTextWriter nesnesi ile &quot;Personeller.xml&quot; ad\u0131ndaki XML dosyas\u0131n\u0131n nereye olusturulaca\u011f\u0131n\u0131 belirledik.\r\n            \/\/Encoding.GetEncoding(1254) = T\u00fcrk\u00e7e karakter dil koduna g\u00f6re XML dosyas\u0131n\u0131 encode etmesini s\u00f6yledik.\r\n            XMLDosyasi.Formatting = Formatting.Indented;\r\n            \/\/Formatting \u00f6zelli\u011fi, XML nodelar\u0131n\u0131n girintili yaz\u0131laca\u011f\u0131n\u0131 belirtir.\r\n            XMLDosyasi.WriteStartDocument();\r\n            \/\/XML dosyas\u0131na yaz\u0131lmas\u0131na ba\u015flayaca\u011f\u0131n\u0131 bildirdik.\r\n            XMLDosyasi.WriteComment(&quot;Burada personellerimiz listelenmektedir.&quot;);\r\n            \/\/Bu metod ile XML dosyam\u0131za yorum(Comment) eklenmesi sa\u011flan\u0131r.\r\n            \/\/Bundan sonra elemanlar\u0131 yazd\u0131rmaya ba\u015flayal\u0131m...\r\n            XMLDosyasi.WriteStartElement(&quot;Personeller&quot;);\r\n            \/\/Bu metod ile XML dosyas\u0131n\u0131n root eleman\u0131 belirleniyor.\r\n            while (dr.Read())\r\n            {\r\n                XMLDosyasi.WriteStartElement(&quot;Personel&quot;);\r\n                \/\/WriteStartElement() metodu ile yaz\u0131lan ilk eleman root eleman olur.Daha sonrakiler root un i\u00e7indeki ana nodelar olur.\r\n                XMLDosyasi.WriteAttributeString(&quot;No&quot;, dr&#x5B;&quot;PersonelID&quot;].ToString());\r\n                \/\/WriteAttributeString() metodu ile herhangi bir node a Attribute yaz\u0131labilir.\r\n                for (int i = 0; i &lt; dr.FieldCount; i++)\r\n                {\r\n                    XMLDosyasi.WriteElementString(dr.GetName(i), dr&#x5B;i].ToString());\r\n                    \/\/Node lar\u0131 getirtiyoruz.\r\n                }\r\n                XMLDosyasi.WriteEndElement();\r\n                \/\/Olu\u015fturulan ana node kapat\u0131l\u0131yor.\r\n            }\r\n            XMLDosyasi.WriteEndElement();\r\n            \/\/Root eleman kapat\u0131l\u0131yor.\r\n            XMLDosyasi.WriteEndDocument();\r\n            \/\/XML sayfas\u0131n\u0131n yazma i\u015flevi bitti.\r\n            XMLDosyasi.Close();\r\n            baglanti.Close();\r\n            MessageBox.Show(&quot;\u0130\u015flem Tamamland\u0131.&quot;);\r\n<\/pre>\n<p>Sonu\u00e7 olarak,<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/05\/xml.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-760\" title=\"xml\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/05\/xml.jpg\" alt=\"\" width=\"573\" height=\"572\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/05\/xml.jpg 573w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/05\/xml-150x150.jpg 150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/05\/xml-300x300.jpg 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/a>\u0130yi \u00e7al\u0131\u015fmalar \ud83d\ude42<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Bu yaz\u0131mda C# kodlar\u0131yla XML dosyas\u0131 olu\u015fturmay\u0131 g\u00f6rece\u011fiz.<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,234],"tags":[236,238,240,242,241,237,239,752,235],"class_list":["post-759","post","type-post","status-publish","format-standard","hentry","category-c-sharp-c","category-xml","tag-formatting","tag-writecomment","tag-writeelementstring","tag-writeenddocument","tag-writeendelement","tag-writestartdocument","tag-writestartelement","tag-xml","tag-xmltextwriter"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=759"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/759\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}