2010-06-30 3 views
0

가능하면이 작업을 올바르게 수행하고 싶습니다.XNA 4.0 프로젝트에 XML 데이터를로드하는 데 도움이 필요합니다.

<?xml version="1.0" encoding="utf-8"?> 
    <XnaContent> 
     <Asset Type="PG2.Dictionary"> 
      <Letters TotalInstances="460100"> 
       <Letter Count="34481">&#97;</Letter> 
       ... 
       <Letter Count="1361">&#122;</Letter> 
      </Letters> 
      <Words Count="60516"> 
       <Word>aardvark</Word> 
       ... 
       <Word>zebra</Word> 
      </Words> 
     </Asset> 
    </XnaContent> 

을 나는 사람이 하나 지침을 도와 드릴까요이

namespace PG2 
{ 
    public class Dictionary 
    { 
     public class Letters 
     { 
      public int totalInstances; 

      public List<Character> characters; 

      public class Character 
      { 
       public int count; 
       public char character; 
      } 
     } 

     public class Words 
     { 
      public int count; 
      public HashSet<string> words; 
     } 

     Letters letters; 
     Words words; 
    } 
} 

중 하나에 (사용 Content.Load < 사전>)에서이를로드 좋아하거나 것 다음과 같이 나는 XML 데이터가 자습서에 대한 포인터? 나는 가까이에 오는 몇 가지를 찾았습니다. 그러나 물건들은 3.1과 4.0 사이에서 약간 이해가 안되는 방식으로 약간 바뀌었을 것 같습니다. 그리고 많은 문서들은 내가 가지고 있지 않은 지식을 가정합니다. 지금까지의 이해는 Dictionary 클래스를 Serializable로 만들어야하지만, 그렇게 할 수는 없다. 콘텐츠 프로젝트에 XML 파일을 추가했지만 올바른 XNB 파일을 만들려면 어떻게해야합니까?

감사합니다. Charlie.

답변

1

이것은 도움이 될 수 있습니다 http://blogs.msdn.com/b/shawnhar/archive/2009/03/25/automatic-xnb-serialization-in-xna-game-studio-3-1.aspx. 내 xml 데이터가 올바르게 정의되었는지 확인하려면 다른 방법으로 작업하는 것이 유용하다는 것을 알게되었습니다. 사전 클래스를 인스턴스화하여 모든 필드를 설정 한 다음 XmlSerializer를 사용하여 xml에 직렬화하여 출력을 확인합니다.

+0

감사합니다. 흥미 롭습니다.하지만 XML 데이터를 객체와 정확히 일치하지 않는 형식으로 처리 할 수 ​​있어야합니다. ContentReader/Writer 패턴을 사용해야 할 것 같지만 여전히 4.0에 존재합니까? 워드 프로세서가 조금 따라 잡는 것 같습니다. –

0

Dictionary 클래스에 대해 ContentTypeSerializer를 구현해야합니다. 이 내용을 내용 확장 라이브러리에 넣고 내용 확장 라이브러리에 대한 참조를 내용 프로젝트에 추가합니다. Dictionary 클래스를 게임 및 콘텐츠 확장 프로젝트에서 참조하는 게임 라이브러리에 넣습니다.

참조 : 여기 http://blogs.msdn.com/b/shawnhar/archive/2008/08/26/customizing-intermediateserializer-part-2.aspx

내가 그 당신의 사전 클래스를 직렬화됩니다 썼다 빠른 ContentTypeSerializer이다. 더 나은 오류 처리를 사용할 수 있습니다.

using System; 
using System.Collections.Generic; 
using System.Xml; 
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate; 

namespace PG2 
{ 
    [ContentTypeSerializer] 
    class DictionaryXmlSerializer : ContentTypeSerializer<Dictionary> 
    { 
     private void ReadToNextElement(XmlReader reader) 
     { 
      reader.Read(); 

      while (reader.NodeType != System.Xml.XmlNodeType.Element) 
      { 
       if (!reader.Read()) 
       { 
        return; 
       } 
      } 
     } 

     private void ReadToEndElement(XmlReader reader) 
     { 
      reader.Read(); 

      while (reader.NodeType != System.Xml.XmlNodeType.EndElement) 
      { 
       reader.Read(); 
      } 
     } 

     private int ReadAttributeInt(XmlReader reader, string attributeName) 
     { 
      reader.MoveToAttribute(attributeName); 
      return int.Parse(reader.Value); 
     } 

     protected override Dictionary Deserialize(IntermediateReader input, Microsoft.Xna.Framework.Content.ContentSerializerAttribute format, Dictionary existingInstance) 
     { 
      Dictionary dictionary = new Dictionary(); 
      dictionary.letters = new Dictionary.Letters(); 
      dictionary.letters.characters = new List<Dictionary.Letters.Character>(); 
      dictionary.words = new Dictionary.Words(); 
      dictionary.words.words = new HashSet<string>(); 

      ReadToNextElement(input.Xml); 
      dictionary.letters.totalInstances = ReadAttributeInt(input.Xml, "TotalInstances"); 

      ReadToNextElement(input.Xml); 

      while (input.Xml.Name == "Letter") 
      { 
       Dictionary.Letters.Character character = new Dictionary.Letters.Character(); 

       character.count = ReadAttributeInt(input.Xml, "Count"); 

       input.Xml.Read(); 
       character.character = input.Xml.Value[0]; 

       dictionary.letters.characters.Add(character); 
       ReadToNextElement(input.Xml); 
      } 

      dictionary.words.count = ReadAttributeInt(input.Xml, "Count"); 

      for (int i = 0; i < dictionary.words.count; i++) 
      { 
       ReadToNextElement(input.Xml); 
       input.Xml.Read(); 
       dictionary.words.words.Add(input.Xml.Value); 
       ReadToEndElement(input.Xml); 
      } 

      ReadToEndElement(input.Xml); // read to the end of words 
      ReadToEndElement(input.Xml); // read to the end of asset 

      return dictionary; 
     } 

     protected override void Serialize(IntermediateWriter output, Dictionary value, Microsoft.Xna.Framework.Content.ContentSerializerAttribute format) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 
관련 문제