2015-01-21 3 views
0

그래서 XML 파일을 deserialize하는 데 문제가 있습니다.
다음 구조체를 사용하고 있습니다. Strange XmlSerializer 동작

[Serializable] 
public struct GraphicsOptions 
{ 
    public int Height; 
    public int Width; 
    public bool Fullscreen; 
    public bool AntiAliasing; 
    public int ClickResCount; 
} 

그리고 만들려면 다음 코드

, 세 번째 이후를 제외하고

public void CreateData() 
{ 
    graphicsOptions.Height = graphics.PreferredBackBufferHeight; 
    graphicsOptions.Width = graphics.PreferredBackBufferWidth; 
    graphicsOptions.Fullscreen = graphics.IsFullScreen; 
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling; 
    graphicsOptions.ClickResCount = 1; 
    dataStream = File.Create(SavegamePath); 
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions)); 
    serializer.Serialize(dataStream, graphicsOptions); 
    dataStream.Close(); 
} 

변화와

private void ApplyChanges() 
{ 
    graphicsOptions.Height = graphics.PreferredBackBufferHeight; 
    graphicsOptions.Width = graphics.PreferredBackBufferWidth; 
    graphicsOptions.Fullscreen = graphics.IsFullScreen; 
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling; 
    graphicsOptions.ClickResCount = clickCountResolution; 
    dataStream = File.Open(SavegamePath, FileMode.Open); 
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions)); 
    serializer.Serialize(dataStream, graphicsOptions); 
    dataStream.Close(); 
} 

로드 XML 파일

public void LoadData() 
{ 
    dataStream = File.Open(SavegamePath, FileMode.Open); 
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions)); 
    graphicsOptions = (GraphicsOptions)serializer.Deserialize(dataStream); 
    dataStream.Close(); 
} 

꽤 표준 물건, 나는 그것의 변화를 적용한다. 파일 끝 부분에 2자를 추가합니다. "s>".

이유는 모르겠지만이 정보를 내 구조체에로드 할 수 없으므로 XML을 실질적으로 쓸모 없게 만듭니다.
Visual Studio에서 을 제공합니다. InvalidOperationException (왜 이렇게하는지 이해합니다).

발생을 방지하는 방법에 대한 조언이나 아이디어 또는 예외를 잡으면 2자를 삭제하는 방법은 무엇입니까?

+0

참조하십시오. –

+0

너무 많은 정보를 주었거나 충분하지 못했습니까? 문제의 프로그램은 XNA 프레임 워크로 만들었지 만 업데이트 논리를 추가 할 수는 있지만 실제로는 더 이상 정보를주지 못합니다. – Matonster

+0

혼합 - 너무 많은 (즉, 쓸데없는 물건)과 충분하지 않은 (짧지 만 완전한 프로그램은 아닙니다. 문제). XNA는 아마 * 관련이 없습니다.하지만 문제를 보여주는 짧은 콘솔 앱 (스 니펫이 아닌 전체 프로그램)을 만들 것인지 쉽게 알 수 있습니다. –

답변

1

새 파일이 이전보다 짧아서 그런 현상이 발생합니다. 당신은 다음과 같아야합니다 귀하의 저장 기능을 대신 Open/OpenOrCreate

FileMode.Create를 사용해야합니다 ::

private void ApplyChanges() 
{ 
    graphicsOptions.Height = graphics.PreferredBackBufferHeight; 
    graphicsOptions.Width = graphics.PreferredBackBufferWidth; 
    graphicsOptions.Fullscreen = graphics.IsFullScreen; 
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling; 
    graphicsOptions.ClickResCount = clickCountResolution; 
    using(dataStream = File.Open(SavegamePath, FileMode.Create)) 
    { 
     XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions)); 
     serializer.Serialize(dataStream, graphicsOptions); 
    } 
} 
+0

그것은 내게 그 옵션을주지 않습니다. – Matonster

+0

죄송합니다. 실수는 아닙니다. https://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx – MNie

+0

도와 줘서 고마워, 내 문제가 해결 된 – Matonster

0

변경 ApplyChanges() 메소드를 FileMode.Create 같은 다른 일 모드 사용 할 수 있습니다. 아래에서 올바른 방법을 찾을 수 있습니다. 정말, 단지 파일을 저장할 필요 -

private void ApplyChanges() 
{ 
    graphicsOptions.Height = graphics.PreferredBackBufferHeight; 
    graphicsOptions.Width = graphics.PreferredBackBufferWidth; 
    graphicsOptions.Fullscreen = graphics.IsFullScreen; 
    graphicsOptions.AntiAliasing = graphics.PreferMultiSampling; 
    graphicsOptions.ClickResCount = clickCountResolution; 
    dataStream = File.Open(SavegamePath, FileMode.Create); // You can use FileMode.Truncate as well. 
    XmlSerializer serializer = new XmlSerializer(typeof(GraphicsOptions)); 
    serializer.Serialize(dataStream, graphicsOptions); 
    dataStream.Close(); 
    } 

는 단지 우리에게 문제를 보여주는 짧은 있지만 완벽한 프로그램을 제공하려는 경우 정말 도움이 될 https://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx