Tags: , , , , | Categories: Genel, Programlama Posted by okutbay on 23.02.2010 16:49 | Yorumlar (0)

This is sample application to serializes sample object

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Text;

    4 

    5 using System.IO;

    6 using System.Xml.Serialization;

    7 

    8 namespace ConsoleApplication1

    9 {

   10     class Program

   11     {

   12         static void Main(string[] args)

   13         {

   14             SampleObject sampleObject = new SampleObject();

   15             sampleObject.Id = 35;

   16             sampleObject.Name = "Ozan K. BAYRAM";

   17 

   18             XmlSerializer serializer2 = new XmlSerializer(typeof(SampleObject));

   19             MemoryStream ms = new MemoryStream();

   20             XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();

   21             xmlSerializerNamespaces.Add("", ""); //if you want namespace for your XML define here

   22             serializer2.Serialize(ms, sampleObject, xmlSerializerNamespaces);

   23             StringBuilder XMLText = new StringBuilder();

   24             ms.Position = 0;

   25             using (StreamReader Reader = new StreamReader(ms))

   26             {

   27                 XMLText.Append(Reader.ReadToEnd());

   28             }

   29 

   30             string serializedObject = XMLText.ToString();

   31         }

   32     }

   33 }

Output string

xmlserializationoutput

 

Sample class to use

    1 [Serializable]

    2 [System.Xml.Serialization.XmlRoot("sampleobject")]

    3 public class SampleObject

    4 {

    5     [System.Xml.Serialization.XmlAttribute("id")]

    6     public int Id { get; set; }

    7 

    8     [System.Xml.Serialization.XmlElement("name")]

    9     public string Name { get; set; }

   10 }

Bu yazıyı ilk değerlendiren siz olun

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Yorumlar

Yorum ekle


(Gravatar simgesini gösterecek)  

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading