0

I created an .xsd from an xml file using XSD.Exe from the Visual Studio Tools (now Win 7 SDK Tools). My .xsd file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SceneFile" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"   xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Scene">
<xs:complexType>
  <xs:sequence>
    <xs:element name="VNESceneName" type="xs:string" minOccurs="0" maxOccurs="1"/>
    <xs:element name="VNEPlayerName" type="xs:string" minOccurs="0" maxOccurs="1"/>
    <xs:element name="VNEButtons" minOccurs="0" maxOccurs="1">

(cutted sicne it's too long) I then used XSD2Code (an add-in for VS) to create an .Designer.cs file:

namespace VNEngine
{
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Collections.Generic;


public partial class Scene
{

    private string vNESceneNameField;
    private string vNEPlayerNameField;
    private List<SceneVNEButton> vNEButtonsField;
    private SceneVNETextBox vNETextBoxField;
    private SceneVNEBackground vNEBackgroundField;
    private List<SceneVNESprite> vNESpritesField;
    private SceneVNEMusic vNEMusicField;
    private static System.Xml.Serialization.XmlSerializer serializer;

    public Scene()
    {
        this.vNEMusicField = new SceneVNEMusic();
        this.vNESpritesField = new List<SceneVNESprite>();
        this.vNEBackgroundField = new SceneVNEBackground();
        this.vNETextBoxField = new SceneVNETextBox();
        this.vNEButtonsField = new List<SceneVNEButton>();
    }

    public string VNESceneName
    {
        get
        {
            return this.vNESceneNameField;
        }
        set
        {
            this.vNESceneNameField = value;
        }
    }

(cutted sicne it's too long)

Now I want to create a "Scene"(the root xml tag) in my C# code:

            Scene testscene = new Scene();
            testscene.VNEPlayerName = "hallo";

All up to this point is Working, but when i want to save or Serialize (or Deserialize) like this:

            testscene.SaveToFile(@"Content/Scenes/testscene.xml");

And now when executing I get an Error at the Line (in SceneFiles.designer.cs)

    private static System.Xml.Serialization.XmlSerializer Serializer
    {
        get
        {
            if((serializer == null))
            {
       --->  serializer = new System.Xml.Serialization.XmlSerializer(typeof(Scene));
            }
            return serializer;
        }
    }

And my Error goes like this:

//

Eine nicht behandelte Ausnahme des Typs "System.InvalidOperationException" ist in System.Xml.dll aufgetreten.

Zusätzliche Informationen: Fehler beim Reflektieren des Typs 'VNEngine.Scene'.

//

My rough english translation:

A not handled Exception: System.InvalidOperationException appeared in System.Xml.dll Additional Information: Error at Reflecting the type 'VNEngine.Scene'

//

Does anyone know what I made wrong?

The same thing worked with an smaller not as complex and not with xsd.exe generatet .xsd stylesheet.

4

1 回答 1

0

好的,我找到了,XSD2Code 确实有一个选项 GenerateXMLAttributes 需要设置为 true。

于 2012-08-27T08:09:29.637 回答