Processing vertices -- loading data from XML files

Source: Internet
Author: User
Tags xml example
Problem

You want to load data from an XML file to an xNa project. You can use the default. Net file IO function to read files when the xNa project is started, but this does not work on the xbox360 platform.

You want to use the content pipeline to serialize an XML file into a binary file so that you can read the content contained in these files in the xNa project.

Solution

In an XML file, you just need to insert the object you want to load into the <xnacontent> and <asset> labels. The following XML example file is a custom mapdata Class Object:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Xnacontent?> <Asset type = "xmldatapline. mapdata"?> <Mapname> battle in the middle </mapname> <numberofcastles> 8 </numberofcastles> <allies> <item> humans </item> <item> elves </item> <item> dwarves </item> </allies> </asset> </xnacontent>

TIPS:If you do not know how to automatically create an XML file from an object, you can learn the method at the end of this tutorial.

The xNa framework self-contained content importer can convert an XML file into an object defined in an XML file.

Because the object is nearly established, you do not need to use a processor. The object can be serialized as a binary file immediately. The object here is a custom mapdata class, so you need to define a custom typewriter and typereader.

Note:If the content pipeline knows how to serialize/deserialize objects described in XML files, you do not need to write a new typewriter or typereader.

Figure 5-25 shows the simplified diagram.

Figure 5-25 No processor is required when importing an object from an XML file.

Working Principle

Import A. xml file to the xNa project. In the solution browser, select a file and its properties are displayed in the lower right corner of the screen. On the surface, you want to use the default XML content importer. Selecting no processing required indicates that the output of the importer is serialized as a file without the processor. Figure 5-26 shows the final Properties window.

Figure 5-26 attributes of an imported XML file

If the XML file contains a class object that the content pipeline knows how to serialize/deserialize, you can load this object to a variable in the loadcontent method. However, this tutorial is a custom class, so you need to define a typewriter.

Add a content pipeline to the solution as explained in Step 4-15 of the tutorial. This time, you don't need to define the processor. You only need to define the mapdata class and the corresponding typewriter and typereader.

Note:Make sure to add a reference to the content pipeline project. For more information, see tutorial 4-15. This is necessary, so that your masterProgramTo access the definition of the mapdata class, which is stored in the content pipeline project.

Custom mapdata class

In this example, the mapdata class contains a string, an int, and a string set. Add this definition to the content pipeline project:

Public class mapdata {Public String mapname; Public int numberofcastles; public list <string> allies = new list <string> ();}

TIPS:Verify that the data contained in the preceding XML file provides the data of these three variables.

Define a typewriter that serializes mapdata objects.

As described in tutorial 4-15, typewriter serializes enough data from the object so that the object can be rebuilt by typereader. As in the past, the location of typereader still needs to be provided:

 
[Contenttypewriter] public class mapdatatypewriter: contenttypewriter <mapdata> {protected override void write (contentwriter output, mapdata value) {output. writeobject <string> (value. mapname); output. writeobject <int> (value. numberofcastles); output. writeobject <list <string> (value. allies);} public override string getruntimereader (targetplatform) {return typeof (mapdatareader ). assemblyqualifiedname ;}

You have indicated that this typewriter can serialize mapdata class objects. The default content pipeline knows how to serialize a string, an int, and a list, so you just need to serialize them into a binary file. You passed a mapdatareader typereader link, which will be defined below.

Define a typereader that serializes mapdata objects

Typereader simply creates a new mapdata object and reads string, Int, and list (in the correct order !), And store them in the mapdata object. This object is returned and sent to the xNa game project.

 
Class mapdatareader: contenttypereader <mapdata> {protected override mapdata read (contentreader input, mapdata existinginstance) {mapdata map = new mapdata (); map. mapname = input. readobject <string> (); map. numberofcastles = input. readobject <int> (); map. allies = input. readobject <list <string> (); Return map ;}}
Usage

The first line of the loadcontent MethodCodeYou can read data from the mapdata object in real time. The second line of code only adds a breakpoint to the Code, so that you can check whether the data is correct:

Protected override void loadcontent () {mapdata loadedmap = content. Load <mapdata> ("data"); system. Diagnostics. Debugger. Break ();}

Note:As long as the project is compiled, the XML file is converted into a binary file. When the project starts, only typereader is called to build a mapdata object from the binary file. This means that if you change the XML content, you must recompile it.

Code

Custom content pipelines only contain class definitions, typewriter and typereader, which have been written before the tutorial.

Extended reading: Creating an xNa available XML file from an existing object

This section explains how to store any Class Object in an XML file, and then you can use the default XML importer to load it. First, add the following namespace:

 
Using system. xml; using Microsoft. xNa. Framework. content. pipeline. serialization. Intermediate;

You also need to add references for system. xml and Microsoft. xNa. Framework. content. pipeline. You can add these references by selecting add reference from the project menu.

Then, either link to the custom content pipeline or manually redefine the mapdata class by placing the following code outside the project namespace.

Namespace xmldatapline {public class mapdata {Public String mapname; Public int numberofcastles; public list <string> allies = new list <string> ();}}

If you manually redefine the class, make sure that it is placed in the same namespace as the custom content pipeline (called xmldatapline in my tutorial), and all the variables are the same.

Next, return to the namespace of the xNa project to ensure that a mapdata class object is near:

 
Xmldatapline. mapdata mymap = new xmldatapline. mapdata (); mymap. mapname = "battle in the middle"; mymap. numberofcastles = 8; mymap. allies. add ("humans"); mymap. allies. add ("Elves"); mymap. allies. add ("dwarves ");

Then use this code to store it in an XML file available for xNa, called data. xml:

 
String filename = "data. XML "; xmlwriter writer = xmlwriter. create (filename); intermediateserializer. serialize <xmldatapline. mapdata> (writer, mymap, filename); writer. close ();

When you run the program, the data. xml file will be created in the same location as. EXE.

Note:You can also find this code in the example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.