XNA使用XML

整个周末都奉献给了XNA和XML和《生活大爆炸》。

虽然很想写一篇总结的,但是等不及去实现新的创意了.于是只好将MSDN上相关的内容直接COPY到这里了.简单易用哦.

Adding an XML Content File to a Visual Studio Project

Describes how to add custom game data as an XML file through the Content Pipeline.

Adding XML files to game content

Custom game data that is expressed in an XML format can be easily integrated into your game through the XNA Game Studio Content Pipeline.

This example demonstrates the procedure for integrating custom XML data into the content project of a simple XNA Game Studio game for the Windows platform.

To define game data

Within the XNA Game Studio solution, you create a new Windows Game Library project.

  1. Right-click the solution node, point to Add, click New Project, and then select the Windows Game Library template.

    Ff604979.note(en-us,XNAGameStudio.41).gifNote

    A Windows Game Library project is created instead of a Content Pipeline Extension Library project so that the class we will define can be used by both the content importer that runs at build-time and the content loader at game runtime.

  2. In the Name box, type MyDataTypes, and then click OK.

  3. In Solution Explorer, double-click Class1.cs to edit the new library project file.

  4. Replace the default template with the following code to define the class.

    C#
    namespace MyDataTypes
    {
        public class PetData
        {
            public string Name;
            public string Species;
            public float Weight;
            public int Age;
        }
    }
    

To add an XML file to game content

In this procedure, the "MyDataTypes" library is added as a reference in the content project.

  1. In Solution Explorer, right-click the game content project, point to Add Reference, click the Projects tab, select MyDataTypes, and then click OK.

    A new content item is created next.

  2. In Solution Explorer, right-click the game content folder, point to Add, and then click New Item.

  3. In the Add New Item dialog box, select XML file (.xml) in the Templates pane.

  4. In the Name box, type pets as the file name, and then click OK.

    The new "pets.xml" file automatically opens for editing.

  5. Replace the contents of the template file with the following XML code:

    XML
    <?xml version="1.0" encoding="utf-8" ?>
    <XnaContent>
      <Asset Type="MyDataTypes.PetData[]">
        <Item>
          <Name>Fifi</Name>
          <Species>Dog</Species>
          <Weight>11</Weight>
          <Age>6</Age>
        </Item>
        <Item>
          <Name>Bruno</Name>
          <Species>Dog</Species>
          <Weight>21</Weight>
          <Age>12</Age>
        </Item>
        <Item>
          <Name>Chloe</Name>
          <Species>Cat</Species>
          <Weight>6</Weight>
          <Age>3</Age>
        </Item>
        <Item>
          <Name>Pickles</Name>
          <Species>Hamster</Species>
          <Weight>0.4</Weight>
          <Age>1</Age>
        </Item>
      </Asset>
    </XnaContent>        
          
    

When you press F6 to build the solution, it should build successfully, including the custom game content imported from the XML file.

To load the data at runtime, see the tutorial Loading XML Content at Runtime.

Loading XML Content at Runtime

Describes how to load custom game data at game runtime through the Content Pipeline.

Loading custom game content

This example concludes the procedure begun in the tutorial Adding an XML Content File to a Visual Studio Project.

Once custom game data is integrated as game content from an XML file through the Content Pipeline, it exists within your game runtime package in binary format. The data can be loaded at runtime through the ContentManager.

To load the custom data in the game

The "MyDataTypes" library is added as a reference in the game project.

  1. In Solution Explorer, right-click the game project, click Add Reference, click the Projects tab, select MyDataTypes, and then click OK.

  2. In Solution Explorer, double-click Game1.cs to edit it.

  3. Add the using declaration for the MyDataTypes namespace.

    C#
    using MyDataTypes;
    
  4. Add a data declaration for an array of type PetData, the class defined in the "MyDataTypes" library.

    C#
    PetData[] pets;
    
  5. In the LoadContent override function, load the custom content.

    C#
    protected override void LoadContent()
    {
        // Load the pet data table
        pets = Content.Load<PetData[]>("pets");
    }
    

    The custom game data now resides in the array of PetData objects.

XML Elements for XMLImporter

ML Elements

The following base elements are recognized by XmlImporter Class:

Element Parent Children Description
<XnaContent> <Asset> Top-level tag for XNA Content.
<Asset> <XnaContent> <Item> Marks the asset. The Type attribute specifies the corresponding namespace and class of the matching data type.
<Item> <Asset> When the asset contains multiple objects (as in an array), marks a single object within the group. The child elements correspond to the properties of the data type's class definition.

Examples

Example 1: Single Object

This example demonstrates an XML file that defines an asset that consists of a single item (not an array).

Assume that the XML file is to define a single object of data for the class that is defined as:

C#
namespace XMLTest
{
    class MyTest
    {
        public int elf;
        public string hello;
    }
}
        

The XML file that specifies the data that the Content Loader will read into the object would appear as:

XML
<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
  <Asset Type="XMLTest.MyTest">
    <elf>23</elf>
    <hello>Hello World</hello>
  </Asset>
</XnaContent>
      

Example 2: Multiple Objects

This example demonstrates an XML file that defines an asset that is an array of objects.

Assume that the XML file is to define an array of data for the class that is defined as:

C#
namespace MyDataTypes
{
    public class CatData
    {
        public string Name;
        public float Weight;
        public int Lives;
    }
}

The XML file that specifies the data that the Content Loader will read into the object array would appear as:

XML
<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
  <Asset Type="MyDataTypes.CatData[]">
    <Item>
      <Name>Rhys</Name>
      <Weight>17</Weight>
      <Lives>9</Lives>
    </Item>
    <Item>
      <Name>Boo</Name>
      <Weight>11</Weight>
      <Lives>5</Lives>
    </Item>
  </Asset>
</XnaContent>
      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值