Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化从BMFont工具导出的XML时出错_C#_Xml_Fonts_Bitmap_Monogame - Fatal编程技术网

C# 反序列化从BMFont工具导出的XML时出错

C# 反序列化从BMFont工具导出的XML时出错,c#,xml,fonts,bitmap,monogame,C#,Xml,Fonts,Bitmap,Monogame,我尝试使用以下示例: 这个概念似乎是合理的。我正在使用建议的工具生成的.fnt。它看起来格式正确,并且基于类中使用的[Serialization]有意义 但是,每次运行代码时,都会出现以下错误:“System.Xml.dll中发生类型为'System.InvalidOperationException'的未处理异常 其他信息:XML文档中有错误(1,1)。” 我狂热地环顾了StackExchange和google,在来到这里并发布我的问题之前,我尝试了很多事情。在我修改的FontLoader类的

我尝试使用以下示例:

这个概念似乎是合理的。我正在使用建议的工具生成的.fnt。它看起来格式正确,并且基于类中使用的[Serialization]有意义

但是,每次运行代码时,都会出现以下错误:“System.Xml.dll中发生类型为'System.InvalidOperationException'的未处理异常

其他信息:XML文档中有错误(1,1)。”

我狂热地环顾了StackExchange和google,在来到这里并发布我的问题之前,我尝试了很多事情。在我修改的FontLoader类的下面的评论中,我仍然有许多尝试的混乱痕迹,试图解决这个问题。无论它是如何切片的,都会产生相同的错误,甚至更糟

    public class FontLoader
{
    public static FontFile Load(String filename)
    {
           /* XmlSerializer deserializer = new XmlSerializer(typeof(FontFile));
            TextReader textReader = new StreamReader(Encoding.UTF8.GetBytes(filename).ToString());
            FontFile file = (FontFile)deserializer.Deserialize(textReader);
            textReader.Close();
            return file;*/


byte[] bytes = Encoding.UTF8.GetBytes(System.IO.File.ReadAllText(filename));
FontFile myInstance = null;
using (MemoryStream memStream = new MemoryStream(bytes))
{
XmlSerializer tokenSerializer = new XmlSerializer(typeof(FontFile));
myInstance = (FontFile)tokenSerializer.Deserialize(memStream);
Console.WriteLine(myInstance);
return myInstance;
}

/*
        XmlSerializer deserializer = new XmlSerializer(typeof(FontFile));
        TextReader textReader = new StreamReader(filename);
        FontFile file = (FontFile)deserializer.Deserialize(textReader);
        textReader.Close();
        return file;*/

/*         var xml = System.IO.File.ReadAllText(filename);
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FontFile));
            FontFile file = (FontFile)serializer.Deserialize(stream);
            return file;
        }*/

     /*   var writer = new StringWriter();
        var serializer = new XmlSerializer((filename.GetType()));
        serializer.Serialize(writer, filename);
        string xml = writer.ToString();
        return xml;*/

    }
}
作为测试,我使用的字体是Arial,所以应该可以使用。它从以下几行开始:

info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file="arial_0.png"
chars count=191
char id=32   x=155   y=75    width=3     height=1     xoffset=-1    yoffset=31    xadvance=8     page=0  chnl=15
然后用许多具有相同结构的“char”行继续,然后是紧排行,然后是一组相互具有相同结构的“kerning”行,如下所示:

kernings count=91
kerning first=32  second=65  amount=-2  

若最终导致序列化出现问题,我很抱歉提出这个假设,但我并没有看到它。如果有人有解决办法,请告诉我。谢谢。

自从你提到的答案写好后,有些事情已经改变了。你可以使用这个库来做你想做的事情,而不是自己写。不久前我写了一篇详细的文章

你需要做的第一件事是。在PackageManager控制台中运行以下命令

Install-Package MonoGame.Extended -Pre
安装后,您的packages文件夹中将有几个DLL。其中一个需要被MonoGame管道工具引用。最简单的方法是在文本编辑器中手动编辑
Content.mgcb
文件。添加这样的参考线,但确保与当前版本匹配

#-------------------------------- References --------------------------------#

/reference:..\..\packages\MonoGame.Extended.0.3.44-alpha\lib\MonoGame.Extended.Content.Pipeline.dll
最后,您可以将字体文件和纹理添加到MonoGame管道工具中,并像加载任何其他内容一样加载

_bitmapFont = Content.Load<BitmapFont>("my-font");

如果你真的想自己写,你可以把它看作是开源的。

从你最初的问题来看,这个问题其实很明显,但和你一样,我一开始忽略了这个问题

在您将
.fnt
文件发送给我后,我几乎立即发现了问题,因为我将其与先前导出的文件进行了比较。问题是您以错误的格式导出了文件。正如您在问题中所说,您的文件如下所示:

info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file="arial_0.png"
chars count=191
char id=32   x=155   y=75    width=3     height=1     xoffset=-1    yoffset=31    xadvance=8     page=0  chnl=15
<?xml version="1.0"?>
<font>
  <info face="Arial" size="32" bold="1" italic="0" charset="" unicode="1" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1" outline="1"/>
  <common lineHeight="32" base="26" scaleW="256" scaleH="256" pages="1" packed="0" alphaChnl="1" redChnl="0" greenChnl="0" blueChnl="0"/>
  <pages>
    <page id="0" file="arial_0.png" />
  </pages>
  <chars count="191">
但它应该看起来更像这样:

info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file="arial_0.png"
chars count=191
char id=32   x=155   y=75    width=3     height=1     xoffset=-1    yoffset=31    xadvance=8     page=0  chnl=15
<?xml version="1.0"?>
<font>
  <info face="Arial" size="32" bold="1" italic="0" charset="" unicode="1" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1" outline="1"/>
  <common lineHeight="32" base="26" scaleW="256" scaleH="256" pages="1" packed="0" alphaChnl="1" redChnl="0" greenChnl="0" blueChnl="0"/>
  <pages>
    <page id="0" file="arial_0.png" />
  </pages>
  <chars count="191">

一旦你把它们并排比较,差别就显而易见了。第一个文件是纯文本,第二个文件是XML。这发生在我们当中最好的人身上:)

无论如何,要修复它,您只需要在BMFont工具中将文件格式设置为XML

  • 单击选项
  • 单击导出选项
  • 单击对话框底部附近的XML单选按钮

  • 然后再次导出您的文件。

    您好,谢谢。我希望一旦通过下一个问题,我会给出一个可接受的答案,我很高兴能够做到您的扩展承诺的事情。但是现在,我得到了与“松散代码”尝试相同的错误。xml中的1,1处有错误。这是使用BM字体工具新创建的.fnt。重建内容时,错误正好发生。。。png成功,.fnt失败,“BitmapFontImporter”出现意外故障!详细情况是xml文档中1,1处有一个错误,序列化问题……很有趣。你能把字体文件和它的纹理压缩一下,然后贴一个链接,这样我就可以下载了。我看看能不能发现这个问题。谢谢你的关注。我希望您能在这里@user3421630找到文件和问题。很抱歉,过了一段时间才回复您。当你今天早上发送文件时,我正在打电话上班,直到现在才有机会看。我把这个解决方案作为另一个答案贴出来,因为它与第一个答案大不相同,但我认为它们对任何一个在这个问题上发言的人都有价值。随便你喜欢哪一个都行。哦,哇。。。我认为自定义类应该根据预期的模式将纯文本转换为xml,然后反序列化它,这是基于我的假设,即纯文本就是BM Font的工作方式,由.net应用程序将其修复为xml。哈默认为文本,我甚至不知道您可以更改它。非常感谢你!不客气。我们最终可以在MonoGame中支持所有不同的格式。扩展了,但由于只需点击一次,目前还不是优先考虑的。很高兴你能成功:)