XmlSerializer的; System.invalidOperationException

时间:2012-12-12 07:06:24

标签: c# xml xml-serialization

编辑:使用Jon Skeet提供的代码。

我收到以下错误:

Message: There is an error in XML document (2, 2).
Inner Exception: {"<Translator xmlns=''> was not expected."}

如果有帮助我可以提供以下代码:

Translator.cs:

public class Translator
{
    public FullBotTranslation Translation;

    public Translator()
    {
        Translation = new FullBotTranslation();
    }

    public void LoadLanguage(string language)
    {
        if (!Useful.ExistFile(System.AppDomain.CurrentDomain.BaseDirectory + "\\LanguagePacks\\" + language + ".xml"))
            language = "Francais";

        Translation = XmlSerializerHelper.Deserialize<FullBotTranslation>(System.AppDomain.CurrentDomain.BaseDirectory + "\\LanguagePacks\\" + language + ".xml");
    }


    public string GetTranslation(PhraseID phraseId)
    {
        foreach (Phrase phrase in Translation.Phrases)
        {
            if (phrase.PhraseID == phraseId)
                return phrase.PhraseString;
        }

        return "Incomplete translation...";
    }

    #region Nested type: Translation

    [Serializable]
    public class FullBotTranslation
    {
        public List<Phrase> Phrases = new List<Phrase>();
    }

    #endregion
}

Phrase.cs:

public class Phrase
{
    public PhraseID PhraseID { set;  get; }
    public string PhraseString{ set; get; } 

    public Phrase()
    {

    }
}

PhraseID.cs

[Serializable]
public enum PhraseID
{
    none,
    Button_Start,
    Button_Stop,
}

我这样用:

设定:

private Translator _translator;
_translator = new Translator();

保存:

Helpers.XmlSerializerHelper.Serialize(
            System.AppDomain.CurrentDomain.BaseDirectory + "\\LanguagePacks\\" + langPackName.Text + ".xml",
            _translator);

装载:

        _translator = new Translator(); //yes this is needed ;)

        _translator.LoadLanguage(preloadedLangCombo.SelectedItem.ToString());

使用上面的代码保存XML文件时,它会输出以下内容:

English.XML:

<?xml version="1.0"?>
<Translator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Translation>
    <Phrases>
      <Phrase>
        <PhraseID>none</PhraseID>
        <PhraseString>Incomplete Translation</PhraseString>
      </Phrase>
      <Phrase>
        <PhraseID>Button_Start</PhraseID>
        <PhraseString>Start</PhraseString>
      </Phrase>
      <Phrase>
        <PhraseID>Button_Stop</PhraseID>
        <PhraseString>Stop</PhraseString>
      </Phrase>
    </Phrases>
  </Translation>
</Translator>

1 个答案:

答案 0 :(得分:0)

目前还不清楚这里出了什么问题 - 特别是因为错误消息似乎与您的示例XML不匹配。你的异常处理可能会隐藏问题 - 捕获所有类似的异常是一个非常糟糕的主意,并且你将花费不必要的时间来关闭所涉及的流。我会把你的课压缩到这个:

public static class XmlSerializerHelper
{
    public static void Serialize(String path, object @object)
    {
        using (var stream = File.Create(path))
        {
            var s = new XmlSerializer(@object.GetType());
            s.Serialize(stream, @object);
        }
    }

    public static T Deserialize<T>(String path)
    {
        using (var stream = File.OpenRead(path))
        {
            var s = new XmlSerializer(typeof(T));
            return (T) s.Deserialize(stream);
        }
    }
}

现在如果出现问题,异常将从方法中传播出来,而不是伪装。另外,请注意我已经序列化到/只是流,而不是涉及StreamWriter。让XML基础设施处理所有编码更简单。

上面的代码在一个简单的测试中对我很好。