Loading xml file within application?

is it possible to load a xml file within the application itself?

the problem is that xml files are exposed to user editing and seem unable to be edited within the pipeline,
but outside the pipeline they are unable to load, since it searches for them inside the build directory.

XML files can be processed into a binary format by the content pipeline. There have been reports that some XML files don’t build, but we haven’t had enough information provided to determine what exactly is going wrong for those cases. For other cases they work fine.

As for editing XML files, it is best left to your tool of choice for editing XML files. The Pipeline GUI tool is for managing content projects rather than editing assets.

Plain XML files can be loaded by your application just like any other C# application would, using XDocument, XmlDocument or similar.

2 Likes

ya I just found out I get an error when trying to compile xml file in the content pipeline too
but I’m not sure how to copy the error down to tell you

also the pipeline doesn’t allow me to edit the xml files like visual studio does it just brings them up in internet explorer

It would be good to have a sample project that exhibits the problem. Are you able to zip up a small project that shows the issue and make it available?

yes here

Right-click on the file in the Pipeline tool and select Open with. This will allow you to select the tool used to edit the XML file.

Looking at your sample project, I can see a few things immediately.

  • You have a .cs file in the Content directory and added to the mgcb file. I don’t know what this was trying to achieve.
  • XML files to be compiled with the content pipeline need to start with XnaContent and Asset nodes, as in the template XML file if you select New Item in the Pipeline tool and select XML Content.

This is a sample XML asset that would contain a Dictionary<Int, String>.

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <!-- TODO: replace this Asset with your own XML asset data. -->
  <Asset Type="System.Collections.Generic.Dictionary[System.Int32, System.String]">
    <!-- Your asset fields go here -->
  </Asset>
</XnaContent>

This will work without any additional code because all of the types Dictionary, Int32 and String are known to the content pipeline through mscorlib. If you want to use your own types, such as an array of Tile, you will need to have the Tile class in its own class library so it can be accessed by the content pipeline and the game. The compiled class library assembly then needs to be added to the MGCB project as a reference. Select the root node in the treeview in the Pipeline tool and double-click on References in the Properties window.

Now that you have the class library containing the Tile type available to the content pipeline, you need to construct your XML file in a way that the content pipeline can understand.

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="System.Collections.Generic.List[GB_Zelda.Map.Tile]">
    <Item>
      <XPos>1</XPos>
      <YPos>1</YPos>
      <ZPos>1</ZPos>
      <TextureXPos>0</TextureXPos>
      <TextureYPos>0</TextureYPos>
      <TextureName>Overworld_Tiles</TextureName>
    </Item>
  </Asset>
</XnaContent>

This should build into a XNB file. To load this through the ContentManager, use the following

var tiles = Content.Load<List<Tile>>("Xml/Maps/Test_Map");

I have edited your project to show this working, available here. Note that I removed the source control bindings in the solution and project because VS’s source control bindings are annoying when you don’t have access to the source control server they’re bound to.

thankyou so much I will take a look at the project

Edit: I still can’t seem to edit the xml file inside the content pipeline
I can get it to open the content.mgcb file with an xml editor but thats not what I want.
I need to edit the xml file inside it

Edit2: what do you mean by map.cs has to be in ins own class library?
does that mean you can’t store it inside the main project hierarchy?
can you explain some more?

You have to use an external tool, the contentpipeline is only here to build, not edit, transform etc.
You can edit an xml file, with notepad or… visual studio.

You need to share classes between projects with a reference to a dll (a class library) so they are the same, and you only edit code in one place.

How do I include and read an uncompiled XML file? I want to have a user-editable config file that is read at runtime.

You can use IsolatedStorage to save/load the file, it’s available for all platform !

For XML file …

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists("filename.xml"))
{
    IsolatedStorageFileStream fs = store.OpenFile("filename.xml", FileMode.Open);
    using (XmlReader sr = XmlReader.Create(fs))
    {
        // load your data here
    }
}
1 Like