Jason94
Jason94

Reputation: 13610

how to load a xml file in c# windows phone?

Im making a game, where i generate the map from a xml file.

Does not WP7 support regular xml? also tried to wrap it inside a XnaContent xml file, but all my nodes a invalid.

How do I go about to load a regular xml file into my c# WP7 project?

Upvotes: 0

Views: 796

Answers (3)

Etch
Etch

Reputation: 3054

I think Jon Skeet nailed it on the head, but I had an example where I am doing a read of an xml file from isolated memory I thought I would share.

 private const string filePath = "TimeKeeperData.xml";

    private static XDocument ReadDataFromIsolatedStorageXmlDoc()
    {
        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!storage.FileExists(filePath))
            {
                return new XDocument();
            }

            using (var isoFileStream = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, storage))
            {
                using (XmlReader reader = XmlReader.Create(isoFileStream))
                {
                    return XDocument.Load(reader);
                }
            }
        }
    }

Upvotes: 2

ZombieSheep
ZombieSheep

Reputation: 29953

When you say all your nodes are invalid, it makes me think you might not be handling xml namespaces correctly. Are you taking them into account, if required?

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502016

Yes, Windows Phone 7 definitely supports regular XML, and it works fine using LINQ to XML to load XML data either from isolated storage, or fetched from the web, or fetched from a resource.

It's unclear exactly what you're trying to do or what's going wrong (partly because you've shown no code) but you can certainly use XML in Windows Phone 7. Not being an XNA developer, I don't know about XnaContent, but you should potentially try loading it as an XDocument first just to check whether that works, and go from there.

Upvotes: 2

Related Questions