1

there my XMl file :

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="XnaCzyMap.Element[]">
    <Element>
    <id>5</id>
    <pos_x>54</pos_x>
    <pos_y>30</pos_y>
    <rot>90</rot>
  </Element>
  <Element>
    <id>5</id>
    <pos_x>54</pos_x>
    <pos_y>165</pos_y>
    <rot>90</rot>
  </Element>
  <Element>
    <id>5</id>
    <pos_x>54</pos_x>
    <pos_y>340</pos_y>
    <rot>25</rot>
  </Element>
    </Asset>
</XnaContent>

And my class :

public class Element
{
    [System.Xml.Serialization.XmlElement("id")]
    public int id { get; set; }

    [System.Xml.Serialization.XmlElement("pos_x")]
    public int pos_x { get; set; }

    [System.Xml.Serialization.XmlElement("pos_y")]
    public int pos_y { get; set; }

    [System.Xml.Serialization.XmlElement("rot")]
    public int rot { get; set; }
}

I want to Deserialize my XML as a collection of "Element". it worked when i didn't use XNA, but its not working anymore. VS give me the following error : Error 1 There was an error while deserializing intermediate XML. Cannot find type "XnaCzyMap.Element".

I have tried to add a collection as class but don"t work either.

  [System.Xml.Serialization.XmlRoot("XnaContent")]
    public class Elements
    {
        [XmlElement("Element")]
        public List<Element> listObjet { get; set; }

    }

What is the difference between deserialize using Xna or not ?

The code that do the deserialization :

  public void load_map(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Element[]));

            StreamReader reader = new StreamReader(path);
            try
            {
                file = (Element[])serializer.Deserialize(reader);

            }
            catch (Exception e)
            {
            }
            reader.Close();
        }

I have tried to switch "Element[]" by "Elements" when i tried with "Elements class"


Maybe you forgot to setup pyramid_beaker properly during application startup as mentioned here

If you want more control, follow this way

Remove the parameter below from your settings. It is an option for pyramid.session.UnencryptedCookieSessionFactoryConfig. You switched to using pyramid_beaker.

session.cookie_max_age = 120
4

1 回答 1

0

Xna 不会干扰序列化,但您的代码中有小错误。

您的代码正确,但您使用的 XML 文件格式不正确。如果对 XML 文件的预期格式有疑问,请尝试使用 XmlSerializer 的 Serialize 方法来创建预期的格式。

在这种情况下,要完成这项工作,您必须像这样更改 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfElement Type="XnaCzyMap.Element[]">
    <Element>
    <id>5</id>
    <pos_x>54</pos_x>
    <pos_y>30</pos_y>
    <rot>90</rot>
  </Element>
  <Element>
    <id>5</id>
    <pos_x>54</pos_x>
    <pos_y>165</pos_y>
    <rot>90</rot>
  </Element>
  <Element>
    <id>5</id>
    <pos_x>54</pos_x>
    <pos_y>340</pos_y>
    <rot>25</rot>
  </Element>
</ArrayOfElement>

请注意我是如何更改根标签的。它的名称 (ArrayOfElement) 对 .NET 很重要。

编辑:如果您想保留原始 XML 文件,那很好,但您必须更改代码中的几个错误。

  1. XmlSerializer 构造将 typeof(Elements) 作为参数,而不是 typeof(Element[]) 并且还会适当地更改返回类型转换。你提到你这样做了。
  2. 将此属性更改为目标 listObjet 集合 [XmlElement("Element")] 附近的此属性 [XmlElement("Asset")]。此属性并未说明集合成员在 XML 中的外观。它说明了整个系列的名称。
于 2013-10-21T16:20:28.310 回答