XMLNS 파일을 역 직렬화하려고합니다.
<feed xmlns:live="http://www.live.com/marketplace" xmlns="http://www.w3.org/2005/Atom">
<live:totalItems>1177</live:totalItems>
<live:numItems>1</live:numItems>
<title>FindGames Results</title>
<updated>2013-09-19T09:28:02.77Z</updated>
<entry live:itemNum="34" live:detailView="3">
<id>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</id>
<updated>2013-09-19T09:28:02.77Z</updated>
<title>Rayman® Legends</title>
<content type="text">game content</content>
<live:media>
<live:mediaType>1</live:mediaType>
<live:gameTitleMediaId>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</live:gameTitleMediaId>
<live:reducedTitle>Rayman® Legends</live:reducedTitle>
<live:reducedDescription>The Glade of Dreams is in trouble once again! During a 100-year nap, nightmares have multiplied and spread, creating new monsters even scarier than before!</live:reducedDescription>
<live:availabilityDate>2013-06-11T00:00:00</live:availabilityDate>
<live:releaseDate>2013-08-29T00:00:00</live:releaseDate>
<live:ratingId>20</live:ratingId>
<live:developer>Ubisoft</live:developer>
<live:publisher>Ubisoft</live:publisher>
<live:newestOfferStartDate>2013-09-13T09:00:00Z</live:newestOfferStartDate>
<live:totalOfferCount>1</live:totalOfferCount>
<live:titleId>1431505091</live:titleId>
<live:effectiveTitleId>1431505091</live:effectiveTitleId>
<live:gameReducedTitle>Rayman® Legends</live:gameReducedTitle>
<live:ratingAggregate>4.50</live:ratingAggregate>
<live:numberOfRatings>1193</live:numberOfRatings>
</live:media>
</entry>
</feed>
내 현재 코드 : Deserialize 클래스 :
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
public string title { get; set; }
public string totalItems { get; set; }
public string reducedDescription{ get; set; }
public string ratingId { get; set; }
public string developer { get; set; }
public string publisher { get; set; }
public string tittleId { get; set; }
public string ratingAggregate { get; set; }
public string numberOfRatings { get; set; }
public string boxImage { get; set; }
public string categories { get; set; }
}
class deserializeXML
{
public static void deserialize()
{
using (StreamReader reader = new StreamReader("Query.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(entry));
entry x1 = serializer.Deserialize(reader) as entry;
}
}
}
방금 제목을 받았습니다 (Rayman® Legends 대신 FindGames 결과). "entry"와 "live : media"뒤에 매개 변수를 가져와야합니다. 처음에 "live :"로 매개 변수를받는 방법을 모릅니다.
편집하다:
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int numItems { get; set; }
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int totalItems { get; set; }
잘 작동하지만 :
[XmlElement("media" Namespace="http://www.live.com/marketplace")]
public media media{ get; set; }
이 반환 null 미디어 클래스 : /
atom 네임 스페이스에없는 이러한 유형 및 속성을 각각의 네임 스페이스에 명시 적으로 매핑해야합니다. 의 네임 스페이스 선언 XmlRoot
은 루트 요소의 네임 스페이스를 지정합니다. 문서에 여러 네임 스페이스가있는 경우이를 명시 적으로 호출해야합니다.
예를 들어 itemNum
속성을 올바르게 매핑하려면 항목 클래스에서 다음을 시도하십시오.
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
[XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
public int itemNum { get; set; }
public string title { get; set; }
public string totalItems { get; set; }
public string reducedDescription { get; set; }
public string ratingId { get; set; }
public string developer { get; set; }
public string publisher { get; set; }
public string tittleId { get; set; }
public string ratingAggregate { get; set; }
public string numberOfRatings { get; set; }
public string boxImage { get; set; }
public string categories { get; set; }
}
그런 다음 미디어 노드와 같은 것에 매핑 할 추가 유형을 선언해야합니다.
class media
{
public int mediaType{ get; set; }
public string reducedDescription{ get; set; }
etc. etc.
}
그런 다음 모든 "라이브"속성을 엔트리 클래스에서 새 미디어 클래스로 이동해야하며 다음과 같은 엔트리 클래스가 남습니다.
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
[XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
public int itemNum { get; set; }
public string title { get; set; }
[XmlElement("media" Namespace="http://www.live.com/marketplace")]
public media media{ get; set; }
}
클래스 계층 구조가 xml 계층 구조와 정확히 일치 할 필요는 없으며 System.Xml.Serialization의 다양한 속성을 사용하여 Xml의 중요한 재 매핑 및 변환을 수행 할 수 있지만 다음과 같은 경우 이러한 종류의 항목을 구현, 이해 및 유지하는 것이 더 쉽습니다. C # 클래스는 xml 구조에 1 : 1을 매핑합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다