다음을 통해 공유


Feed Formatter (JSON)

이 샘플에서는 사용자 지정 SyndicationFeedFormatterDataContractJsonSerializer를 사용하여 JSON(JavaScript Object Notation) 형식으로 SyndicationFeed 클래스의 인스턴스를 serialize하는 방법을 보여 줍니다.

샘플 아키텍처

이 샘플에서는 SyndicationFeedFormatter에서 상속되는 JsonFeedFormatter라는 클래스를 구현합니다. JsonFeedFormatter 클래스는 DataContractJsonSerializer를 사용하여 JSON 형식으로 데이터를 읽고 씁니다. 내부적으로 포맷터는 JsonSyndicationFeedJsonSyndicationItem이라는 데이터 계약 형식의 사용자 지정 집합을 사용하여 serializer에 의해 생성되는 JSON 데이터의 형식을 제어합니다. 이러한 구현 정보는 최종 사용자에게 표시되지 않으므로 표준 SyndicationFeedSyndicationItem 클래스를 호출할 수 있습니다.

JSON 피드 쓰기

다음 샘플 코드와 같이 이 샘플에 구현된 JsonFeedFormatterDataContractJsonSerializer와 함께 사용하여 JSON 피드를 쓸 수 있습니다.

//Basic feed with sample data
SyndicationFeed feed = new SyndicationFeed("Custom JSON feed", "A Syndication extensibility sample", null);
feed.LastUpdatedTime = DateTime.Now;
feed.Items = from s in new string[] { "hello", "world" }
select new SyndicationItem()
{
    Summary = SyndicationContent.CreatePlaintextContent(s)
};

//Write the feed out to a MemoryStream in JSON format
DataContractJsonSerializer writeSerializer = new DataContractJsonSerializer(typeof(JsonFeedFormatter));
writeSerializer.WriteObject(stream, new JsonFeedFormatter(feed));

JSON 필드 읽기

다음 코드와 같이JsonFeedFormatter를 사용하여 JSON 형식의 데이터 스트림에서 SyndicationFeed를 가져올 수 있습니다.

//Read in the feed using the DataContractJsonSerializer

DataContractJsonSerializer readSerializer = new DataContractJsonSerializer(typeof(JsonFeedFormatter));

JsonFeedFormatter formatter = readSerializer.ReadObject(stream) as JsonFeedFormatter;

SyndicationFeed feedRead = formatter.Feed;

샘플을 설치, 빌드 및 실행하려면

  1. Windows Communication Foundation 샘플의 일회 설치 절차를 수행했는지 확인합니다.

  2. C# 또는 Visual Basic .NET 버전의 솔루션을 빌드하려면 Windows Communication Foundation 샘플 빌드의 지침을 따릅니다.

  3. 단일 컴퓨터 또는 다중 컴퓨터 구성에서 샘플을 실행하려면 Running the Windows Communication Foundation Samples의 지침을 따릅니다.

Bb943484.Important(ko-kr,VS.100).gif 참고:
컴퓨터에 이 샘플이 이미 설치되어 있을 수도 있습니다. 계속하기 전에 다음(기본) 디렉터리를 확인하십시오.

<InstallDrive>:\WF_WCF_Samples

이 디렉터리가 없으면 Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4로 이동하여 WCF(Windows Communication Foundation) 및 WF 샘플을 모두 다운로드하십시오. 이 샘플은 다음 디렉터리에 있습니다.

<InstallDrive>:\WF_WCF_Samples\WCF\Extensibility\Syndication\JsonFeeds