검색결과 리스트
tutorial에 해당되는 글 11건
- 2015.07.26 [Hibernate] Hibernate EHCache Second Level Caching Example Tutorial
- 2014.07.13 [Git] Git Tutorials and Training (Atlassian)
- 2014.06.22 [Python] Python Tutorial
- 2013.08.20 [SpringSecurity] Spring Security Full ACL Tutorial
- 2013.05.24 [ATL] ATL Com Programming
- 2013.05.23 [C#] Linq for xml tutorial
- 2013.05.08 [C#] XML Serialize Tutorial
- 2013.04.23 [Node.js] Node.js tutorial
- 2013.01.23 [.Net] Castle DynamicProxy Tutorial
- 2012.10.25 [Eclipse] Plug-In 개발
- 2012.06.15 [Json] 안드로이드 Json 처리
글
[Hibernate] Hibernate EHCache Second Level Caching Example Tutorial
설정
'Java > Hibernate / JPA' 카테고리의 다른 글
[Spring Data JPA] 스프링 데이터 JPA 레퍼런스 번역 (0) | 2015.07.25 |
---|---|
[Hibernate] Configuration-Logging (0) | 2014.08.08 |
[JPA] @TableGenerator (0) | 2014.06.23 |
[Hibernate] hibernate-ehcache (0) | 2014.06.11 |
[Hibernate] Hibernate Performance Optimization (0) | 2014.05.27 |
트랙백
댓글
글
'VCS > Git' 카테고리의 다른 글
[Git] redmine과 Git repository 연동 (1) | 2014.08.05 |
---|---|
[Git] git으로 디버깅 하기 (0) | 2014.07.21 |
[Git] Git Server 설치 (0) | 2014.06.05 |
[Git] Git 명령어 정리 (0) | 2014.06.05 |
[Git] SVN 능력자를 위한 git 개념 가이드 (0) | 2014.04.04 |
트랙백
댓글
글
글
'Java > SpringSecurity' 카테고리의 다른 글
[SpringSecurity] Spring Security Java Config (0) | 2015.08.04 |
---|---|
[SpringSecurity] SpringSecurity Multiple Login Forms 만들기 (0) | 2014.04.18 |
[SpringSecurity] 중복 로그인 제거 (0) | 2014.04.10 |
[SpringSecurity] 스프링 시큐리티, 어디까지 만지작 거릴수 있을까? (0) | 2013.11.23 |
[SpringSecurity] SpringSecurity 3 (0) | 2013.08.08 |
트랙백
댓글
글
너무 정리가 잘되어 있는곳 : http://codecrue.egloos.com/category/ATL%2FActiveX
'C/C++ > VC++ / MFC' 카테고리의 다른 글
[MFC] Dialog 베이스로 시작시 숨기기 (0) | 2013.05.31 |
---|---|
[VC++] Detecting Hardware Insertion and/or Removal (0) | 2013.05.30 |
[COM] Com Event Handling (0) | 2013.05.24 |
[VC++] IOCP 프로그래밍 (1) | 2013.05.21 |
[VC++] Visual Studio Predefine Macro (0) | 2013.05.01 |
트랙백
댓글
글
'.Net > C#' 카테고리의 다른 글
[C#] ref, out의 차이 (0) | 2013.06.12 |
---|---|
[C#] XML Serialize Tutorial (0) | 2013.05.08 |
[C#] Mutex를 통한 다중 인스턴스 실행방지 (0) | 2013.02.08 |
[C#] Visual Studio TODO 만들기 (0) | 2013.01.30 |
[C#] Form close와 Dispose (0) | 2013.01.28 |
트랙백
댓글
글
링크 : http://tech.pro/tutorial/798/csharp-tutorial-xml-serialization
링크 : http://msdn.microsoft.com/ko-kr/library/58a18dwa.aspx
A long while ago we posted a tutorial on how to serialize objects to a binary file. While this is very useful, unfortunately the resulting file is not very human readable. In this tutorial, I'm going to demonstrate how to serialize your own objects to and from an XML file.
Since .NET can use reflection to get property names, basic serialization is unbelievably simple. It only gets slightly difficult when you want to name your XML tags differently than your property names (but still not very hard). If you've ever used an XML serialization package in C++ like boost, tinyXML, or libXML2, you'll see how comparatively easy C# is to use.
Let's start with a basic example. Below is an object that stores some information about a movie.
public class Movie
{
public string Title
{ get; set; }
public int Rating
{ get; set; }
public DateTime ReleaseDate
{ get; set; }
}
All right, now that we have an object, let's write a function that will save it to XML.
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
The first thing I do is create an XMLSerializer (located in the System.Xml.Serialization namespace) that will serialize objects of type Movie. The XMLSerializer will serialize objects to a stream, so we'll have to create one of those next. In this case, I want to serialize it to a file, so I create a TextWriter. I then simply call Serialize on the XMLSerializer passing in the stream (textWriter) and the object (movie). Lastly I close the TextWriter because you should always close opened files. That's it! Let's create a movie object and see how this is used.
static void Main(string[] args)
{
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = 6.9f;
SerializeToXML(movie);
}
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
After this code executes, we'll have an XML file with the contents of our movie object.
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>Starship Troopers</Title>
<Rating>6.9</Rating>
<ReleaseDate>1997-11-07T00:00:00</ReleaseDate>
</Movie>
If you noticed, all of the XML tag names are the same as the property names. If we want to change those, we can simply add an attribute above each property that sets the tag name.
public class Movie
{
[XmlElement("MovieName")]
public string Title
{ get; set; }
[XmlElement("MovieRating")]
public float Rating
{ get; set; }
[XmlElement("MovieReleaseDate")]
public DateTime ReleaseDate
{ get; set; }
}
Now when the same code is executed again, we get our custom tag names.
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MovieName>Starship Troopers</MovieName>
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
Sometimes, in XML, you want information stored as an attribute of another tag instead of a tag by itself. This can be easily accomplished with another property attribute.
public class Movie
{
[XmlAttribute("MovieName")]
public string Title
{ get; set; }
[XmlElement("MovieRating")]
public float Rating
{ get; set; }
[XmlElement("MovieReleaseDate")]
public DateTime ReleaseDate
{ get; set; }
}
With this code, MovieName will now be an attribute on the Movie tag.
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
MovieName="Starship Troopers">
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
Let's move on to something a little more interesting. Let's create another movie and serialize a List of them to our XML file. Here's the modified code to do just that:
static void Main(string[] args)
{
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = 6.9f;
Movie movie2 = new Movie();
movie2.Title = "Ace Ventura: When Nature Calls";
movie2.ReleaseDate = DateTime.Parse("11/10/1995");
movie2.Rating = 5.4f;
List<Movie> movies = new List<Movie>() { movie, movie2 };
SerializeToXML(movies);
}
static public void SerializeToXML(List<Movie> movies)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Movie>));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movies);
textWriter.Close();
}
Now we have XML that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Movie MovieName="Starship Troopers">
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
<Movie MovieName="Ace Ventura: When Nature Calls">
<MovieRating>5.4</MovieRating>
<MovieReleaseDate>1995-11-10T00:00:00</MovieReleaseDate>
</Movie>
</ArrayOfMovie>
Ok, so you can see how easy it is to get your objects into an XML document. Let's now look at how to read an XML document back into our objects - deserialization. The process of deserializing is very similar to what we did for serialization.
static List<Movie> DeserializeFromXML()
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<Movie>));
TextReader textReader = new StreamReader(@"C:\movie.xml");
List<Movie> movies;
movies = (List<Movie>)deserializer.Deserialize(textReader);
textReader.Close();
return movies;
}
Just like before, we first create an XmlSerializer that can deserialize objects of type List\. The XmlSerializer also deserializes from a stream, so we create a file stream from our XML file. We then simply call Deserialize
on the stream and cast the output to our desired type. Now the movies List is populated with objects that we previously serialized to the XML file.
The deserializer is very good at handling missing pieces of information in your XML file. Let's say the second movie didn't have the MovieName attribute on the Movie tag. When the XML file is deserialized, it simply populates that field with null. If MovieRating wasn't there, you'd receive 0. Since a DateTime object can't be null, if MovieReleaseDate was missing, you'd receive DateTime.MinValue (1/1/0001 12:00:00AM).
If the XML document contains invalid syntax, like say the first opening Movie tag was missing, the Deserialize call will fail with an InvalidOperationException. It will also be kind enough to specify the location in the file where it encountered the error (line number, column number).
One thing to remember is that the basic XML serialization won't maintain references. Let's say I populated my movies list with the same movie reference multiple times:
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = 6.9f;
List<Movie> movies = new List<Movie>() { movie, movie };
Now I have a list containing two of the exact same movie reference. When I serialize and deserialize this list, it will be converted to two separate instances of the movie object - they would just have the same information. Along this same line, the XMLSerializer also doesn't support circular references. If you need this kind of flexibility, you should consider binary serialization.
There's still a lot to cover when it comes to XML serialization, but I think this tutorial covers enough of the basics to get things rolling.
'.Net > C#' 카테고리의 다른 글
[C#] ref, out의 차이 (0) | 2013.06.12 |
---|---|
[C#] Linq for xml tutorial (0) | 2013.05.23 |
[C#] Mutex를 통한 다중 인스턴스 실행방지 (0) | 2013.02.08 |
[C#] Visual Studio TODO 만들기 (0) | 2013.01.30 |
[C#] Form close와 Dispose (0) | 2013.01.28 |
트랙백
댓글
글
'JavaScript > NodeJS' 카테고리의 다른 글
[Node.js] Render ejs with express (0) | 2014.01.13 |
---|---|
[Node.js] Express - API 가이드 문서 (0) | 2014.01.13 |
[Node.js] IntelliJ에서 Node 사용시 [ReferenceError: Process is not defined] 발생시 대처 (0) | 2014.01.10 |
[Node.js] Node download (0) | 2014.01.10 |
[Node.js] Node.js 한글 페이지 (0) | 2012.02.09 |
트랙백
댓글
글
링크 : http://kozmic.net/dynamic-proxy-tutorial/
링크 : http://docs.castleproject.org/Tools.DynamicProxy.ashx
'.Net > .Net' 카테고리의 다른 글
[.Net] RSA 암호화 (0) | 2013.03.13 |
---|---|
[.Net] EXE를 포함한 외부 DLL을 같이 배포하기 (0) | 2013.02.19 |
[.Net] NuGet (0) | 2013.01.23 |
[.Net] AOP 프로그래밍 (0) | 2013.01.23 |
[.Net] CodeDomProvider (0) | 2013.01.15 |
트랙백
댓글
글
링크 : http://www.vogella.com/articles/EclipsePlugIn/article.html
링크 : http://www.ibm.com/developerworks/kr/library/os-ecplug/
'IDE/Tool > Eclipse' 카테고리의 다른 글
[Eclipse] 인코딩 에러 Some characters cannot be mapped using "ISO-8859-1" character encoding. (0) | 2013.04.03 |
---|---|
[Eclipse] eclipse.ini 설정 (0) | 2013.04.02 |
[Eclipse] Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library (0) | 2012.10.20 |
[Eclipse] org.eclipse.wst.common.project.facet.core.xml 설정 (0) | 2012.10.20 |
[Eclipse] 이클립스 자바 컴파일 버전 변경 (0) | 2012.10.20 |
트랙백
댓글
글
'Mobile > Android' 카테고리의 다른 글
[Android] YUV420 Format (0) | 2013.08.26 |
---|---|
[dex] Android Decompile (0) | 2013.05.10 |
WebView.addJavascriptInterface 활용 (0) | 2010.12.11 |
[Android] 테트리스 (0) | 2010.12.06 |
RECENT COMMENT