using System.IO;
using System.Xml.Serialization;
namespace Serial
{
// XML Serialzation
public class Person // 클래스는 public 이어야
{
// public 멤버만 저장
public string Name;
public int? Height;
public int Age
{
get { return _age; }
set { if (value >= 0) _age = value; }
}
// private 멤머이므로 저장 안함
private int avgWeight;
private int _age;
public Person()
{
}
public override string ToString()
{
return string.Format("{0}: {1}", Name, Age);
}
}
class PersonTest
{
private string xmlFilename = @"C:\temp\test.xml";
public void SerializePerson()
{
Person objPerson = new Person();
objPerson.Name = "홍길동";
objPerson.Age = 26;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
using (StreamWriter wr = new StreamWriter(xmlFilename))
{
xmlSerializer.Serialize(wr, objPerson);
}
}
public Person DeserializePerson()
{
Person p;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
using (StreamReader rdr = new StreamReader(xmlFilename))
{
p = (Person)xmlSerializer.Deserialize(rdr);
}
return p;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>홍길동</Name>
<Height xsi:nil="true" />
<Age>26</Age>
</Person>
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serial
{
// SoapFormatter Serialzation
[Serializable]
class Person
{
public string Name;
// Soap Serailization은 Generic Type을
// 지원하지 않는다.
// int? 은 Nullable<int>이므로 에러 발생함.
//public int? Height;
public int Age
{
get { return _age; }
set { if (value >= 0) _age = value; }
}
//Serialize를 원하지 않는 필드는
//아래 NonSerialized Attribute를 표시
[NonSerialized] private int avgWeight;
private int _age;
public Person()
{
}
public override string ToString()
{
return string.Format("{0}: {1}", Name, Age);
}
}
class PersonTest
{
private string xmlFilename = @"C:\temp\test.xml";
public void SerializePerson()
{
Person objPerson = new Person();
objPerson.Name = "홍길동";
objPerson.Age = 26;
SoapFormatter soapFmt = new SoapFormatter();
using (FileStream fs = new FileStream(xmlFilename, FileMode.Create))
{
soapFmt.Serialize(fs, objPerson);
}
}
public Person DeserializePerson()
{
Person p;
SoapFormatter soapFmt = new SoapFormatter(); ;
using (FileStream rdr = new FileStream(xmlFilename, FileMode.Open))
{
p = (Person)soapFmt.Deserialize(rdr);
}
return p;
}
}
}
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Person id="ref-1"
xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serial/Serial%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<Name id="ref-3">홍길동</Name>
<_age>26</_age>
</a1:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Serial
{
// BinaryFormatter Serialzation
[Serializable]
class Person
{
public string Name;
// Binary Serailization은 Generic Type을
// 지원한다.
public Nullable<int> Height;
public int Age
{
get { return _age; }
set { if (value >= 0) _age = value; }
}
//Serialize를 원하지 않는 필드는
//아래 NonSerialized Attribute를 표시
[NonSerialized]
private int avgWeight;
private int _age;
public Person()
{
}
public override string ToString()
{
return string.Format("{0}: {1}", Name, Age);
}
}
class PersonTest
{
private string xmlFilename = @"C:\temp\test.dat";
public void SerializePerson()
{
Person objPerson = new Person();
objPerson.Name = "홍길동";
objPerson.Age = 26;
objPerson.Height = 6;
BinaryFormatter binFmt = new BinaryFormatter();
using (FileStream fs = new FileStream(xmlFilename, FileMode.Create))
{
binFmt.Serialize(fs, objPerson);
}
}
public Person DeserializePerson()
{
Person p;
BinaryFormatter binFmt = new BinaryFormatter(); ;
using (FileStream rdr = new FileStream(xmlFilename, FileMode.Open))
{
p = (Person)binFmt.Deserialize(rdr);
}
return p;
}
}
}
[C#] 프로그램 중복실행 방지 ( 단일 프로세스만 실행하기 ) (0) | 2023.07.18 |
---|---|
[C#] 동적 컴파일 (0) | 2023.07.18 |
[C#] Nullable<T> (0) | 2023.07.18 |
[C#] Interface로 구현한 폼 간 값전달 (0) | 2023.07.18 |
[c# 키워드] Lambda Expression => (0) | 2023.07.18 |