在 XNA 上读取 XML 文件
本文关键字:XML 文件 读取 XNA | 更新日期: 2023-09-27 18:37:19
嗨,我无法理解此错误
我想阅读这个 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
<Asset Type="ReadXML.Nod[]">
<Item>
<coneccion>
<Item>
<price>53.6003723</price>
<startNode>
<coneccion />
<position>347 0 228</position>
</startNode>
<endNode>
<coneccion />
<position>334 0 176</position>
</endNode>
</Item>
</coneccion>
<position>347 0 228</position>
</Item>
<Item>
<coneccion>
<Item>
<price>54.74486</price>
<startNode>
<coneccion />
<position>334 0 176</position>
</startNode>
<endNode>
<coneccion />
<position>280 0 167</position>
</endNode>
</Item>
<Item>
<price>53.6003723</price>
<startNode>
<coneccion />
<position>334 0 176</position>
</startNode>
<endNode>
<coneccion />
<position>347 0 228</position>
</endNode>
</Item>
</coneccion>
<position>334 0 176</position>
</Item>
</Asset>
</XnaContent>
我有这门课来阅读:
public class Nod
{
public List<Coneccion> coneccion;
public Vector3 position;
public Nodo(Vector3 p)
{
position = p;
coneccion = new List<Coneccion>();
}
}
和
public class Coneccion
{
public float price;
public Nodo startNode;
public Nodo endNode;
public Coneccion(Nod c, Nod f, float cost)
{
startNode = c;
endNode = f;
price = cost;
}
}
这些类模拟图形。
我必须读取这些名为 level 的 xml 文件.xml我使用这些函数进行读取
Nod[] graph1 = Content.Load<Nod[]>("level.xml");
但是我运行项目并打印下一个错误:
Error 1 Building content threw InvalidOperationException: Cannot deserialize type ReadXML.Nod because it does not have a parameterless constructor.
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ReflectiveSerializer.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.CollectionHelper.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object collection)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ArraySerializer`1.Deserialize(IntermediateReader input, ContentSerializerAttribute format, T[] existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ContentTypeSerializer`1.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObjectInternal[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, Object existingInstance)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format)
en Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateSerializer.Deserialize[T](XmlReader input, String referenceRelocationPath)
en Microsoft.Xna.Framework.Content.Pipeline.XmlImporter.Import(String filename, ContentImporterContext context)
en Microsoft.Xna.Framework.Content.Pipeline.ContentImporter`1.Microsoft.Xna.Framework.Content.Pipeline.IContentImporter.Import(String filename, ContentImporterContext context)
en Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.ImportAssetDirectly(BuildItem item, String importerName)
en Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.ImportAsset(BuildItem item)
en Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAssetWorker(BuildItem item)
en Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAsset(BuildItem item)
en Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.RunTheBuild()
en Microsoft.Xna.Framework.Content.Pipeline.Tasks.BuildContent.RemoteProxy.RunTheBuild(BuildCoordinatorSettings settings, TimestampCache timestampCache, ITaskItem[] sourceAssets, String[]& outputContent, String[]& rebuiltContent, String[]& intermediates, Dictionary`2& dependencyTimestamps, KeyValuePair`2[]& warnings) C:'Downloads'Bomber-O-Man (Entrega 2)'IAJMovimiento'IAJMovimiento'IAJMovimientoContent'level.xml IAJMovimiento
请帮忙
您需要
将此代码添加到Nod
或Nodo
类中(其中一个拼写错误)。
public Nodo()
{
position = Vector3.Zero;
coneccion = new List<Coneccion>();
}
这就是我乍一看看到的,可能还有其他错误。
另外,根据我在这里的理解,您需要更改构造函数调用之外的位置 ContentTypeWriter
.