C# XML Error: System.InvalidOperationException

本文关键字:System InvalidOperationException Error XML | 更新日期: 2023-09-27 18:07:13

序列化XML时出现奇怪的错误:

<?xml version="1.0" encoding="utf-8" ?>
<SplashScreen>
  <Image>
    <Path>Content/splash</Path>
  </Image>
</SplashScreen>
错误:

"类型为'System '的第一次异常。InvalidOperationException"附加信息:反映发生的错误类型。的处理程序此异常,程序可安全继续。"

XMLManager类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace EasyRPG.Managers {
public class XmlManager<T> {
    public Type Type;
    public T Load (String path) {
        T instance;
        using (TextReader reader = new StreamReader(path)){
            XmlSerializer xml = new XmlSerializer(Type);
            instance = (T)xml.Deserialize(reader);
        }
        return instance;
    }
    public void Save (string path, object obj) {
        using (TextWriter writer = new StreamWriter(path)) {
            XmlSerializer xml = new XmlSerializer(Type);
            xml.Serialize(writer, obj);
        }
    }
}
}

我迷路了,我尝试了我所知道的一切(虽然不多),但仍然一无所获。

图像类(如果需要):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using EasyRPG;
namespace TEAM_TheCity.Source {
public class Image {
    public float Alpha;
    public string Path;
    public string Text, FontName;
    public Vector2 Position;
    public Vector2 Scale;
    public Rectangle SourceRect;
    public SpriteFont font;
    public GraphicsDevice GraphcsDevice;
    public Texture2D Texture;
    Vector2 origin;
    ContentManager content;
    RenderTarget2D renderTarget;
    public SpriteBatch SpriteBatch;
    public Image () {
        Path = Text = String.Empty;
        FontName = "Orbitron";
        Position = Vector2.Zero;
        Scale = Vector2.One;
        Alpha = 1.0F;
        SourceRect = Rectangle.Empty;
    }
    public void LoadContent(){
        content = new ContentManager(ScreenManager.Manager.Content.ServiceProvider, "Content");
        if(Path != String.Empty) Texture = content.Load<Texture2D>(Path);
        font = content.Load<SpriteFont>(FontName);
        Vector2 dimensions = Vector2.Zero;
        if(Texture != null)
            dimensions.X += Texture.Width;
        dimensions.X += font.MeasureString(Text).X;
        if(Texture != null)
            dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
        else
            dimensions.Y = font.MeasureString(Text).Y;
        if(SourceRect == Rectangle.Empty)
            SourceRect = new Rectangle(0,0, (int)dimensions.X, (int)dimensions.Y);
        renderTarget = new RenderTarget2D(ScreenManager.Manager.GraphicsDevice,(int) dimensions.X, (int)dimensions.Y);
        ScreenManager.Manager.GraphicsDevice.SetRenderTarget(renderTarget);
        ScreenManager.Manager.GraphicsDevice.Clear(Color.Transparent);
        ScreenManager.Manager.SpriteBatch.Begin();
        if (Texture != null)
            ScreenManager.Manager.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
        ScreenManager.Manager.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
        ScreenManager.Manager.SpriteBatch.End();
        Texture = renderTarget;
        ScreenManager.Manager.GraphicsDevice.SetRenderTarget(null);
    }
    public void UnloadContent(){
    }
    public void Update(GameTime gameTime){
    }
    public void Draw(SpriteBatch SpriteBatch) {
        origin = new Vector2(SourceRect.Width / 2, SourceRect.Height /     2);
        SpriteBatch.Draw(Texture, Position + origin, SourceRect,     Color.White, 0.0f, origin, Scale, SpriteEffects.None, 0.0f);
    }
}
}

和SplashScreen类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using TEAM_TheCity.Source;
namespace EasyRPG {
public class SplashScreen : GameScreen{
    public Image Image;
    public SplashScreen () {
    }
    public override void LoadContent () {
        base.LoadContent();
        Image.LoadContent();
    }
    public override void UnloadContent () {
        base.LoadContent();
        Image.UnloadContent();
    }
    public override void Update (GameTime gameTime) {
        base.Update(gameTime);
        Image.Update(gameTime);
    }
    public override void Draw (SpriteBatch spriteBatch) {
        base.Draw(spriteBatch);
        Image.Draw(spriteBatch);
    }
}
}

GameScreen类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace EasyRPG {
public class GameScreen {
    protected ContentManager content;
    [XmlIgnore]
    public Type Type;
    public GameScreen () {
        Type = this.GetType();
    }
    public virtual void LoadContent () {
        content = new ContentManager(ScreenManager.Manager.Content.ServiceProvider, "Content");
    }
    public virtual void UnloadContent () {
        content.Unload();
    }
    public virtual void Update (GameTime gameTime) {}
    public virtual void Draw (SpriteBatch spriteBatch) {}
}
}

注::抱歉这么多代码,但我是新的XML,我不知道什么是重要的,什么是不

C# XML Error: System.InvalidOperationException

这段代码可以运行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:'temp'test.xml";
        static void Main(string[] args)
        {
            SplashScreen splashScreen = new SplashScreen()
            {
                image = new Image()
                {
                    Path = "Content/splash"
                }
            };
            XmlSerializer serializer = new XmlSerializer(typeof(SplashScreen));
            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, splashScreen);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer xs = new XmlSerializer(typeof(SplashScreen));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            SplashScreen  newSplashScreen = (SplashScreen)xs.Deserialize(reader);
        }
    }
    [XmlRoot("SplashScreen")]
    public class SplashScreen
    {
        [XmlElement("Image")]
        public Image image {get; set; }
    }
    [XmlRoot("Image")]
    public class Image
    {
        [XmlElement("Path")]
        public string Path {get; set;}
    }
}
​

这里有一个答案,不会改变你的代码太多,因为我从youtube教程中认识到它,改变太多可能会使你很难遵循教程。如果在Xml文件中不包含公共变量,则需要使用[XmlIgnore]。因此,您可以更改XMl文件或将[XmlIgnore]添加到image.cs中,因为这是我建议稍后使用的教程,所以如果/当您想要将变量添加到XMl文件中时,请记住删除添加的[XmlIgnore]。

最佳选项:编辑images .cs

[XmlIgnore] public float Alpha;
public string Path;
[XmlIgnore] public string Text, FontName;
[XmlIgnore] public Vector2 Position;
[XmlIgnore] public Vector2 Scale;
[XmlIgnore] public Rectangle SourceRect;
[XmlIgnore] public SpriteFont font;
[XmlIgnore] public GraphicsDevice GraphcsDevice;
[XmlIgnore] public Texture2D Texture;
Vector2 origin;
ContentManager content;
RenderTarget2D renderTarget;
[XmlIgnore] public SpriteBatch SpriteBatch;

其他选项:编辑XML

基本上初始化XML文件中的所有公共值。(注意,我遗漏了一些变量,但您可以理解)

<?xml version="1.0" encoding="utf-8" ?>
<SplashScreen>
  <Image>
    <Alpha>0.5</Alpha>
    <Path>Content/splash</Path>
    <Text>Put Text Here</Text>
    <FontName>Fonts/Orbitron</FontName>
    <Scale>
      <X>1.0</X>
      <Y>1.0</Y>
    </Scale>
    <Position>
      <X>0</X>
      <Y>0</Y>
    </Position>
    <SourceRect>Rectangle.Empty</SourceRect>
  </Image>
</SplashScreen>

边注

我相信你不需要"公共GraphicsDevice GraphicsDevice;"answers"公共SpriteBatch SpriteBatch;"在图像。cs,而不是他们应该在ScreenManager.cs

我也认为"public SpriteFont font;"不应该是公共的。