tags:

views:

2378

answers:

4

I am building a WPF application. Inside that application I am using the XmlReader class to parse several local XML files. The code I have written works perfectly during debugging, but fails once I publish the application and install it.

I have the XML documents as CONTENT in build action, and I have them set to COPY ALWAYS.I can confirm that the XML documents are being deployed in my build and are in tact in the application folder once installed.

What further confuses me is that I am using the same XmlReader code to parse RSS feeds from external websites in this application without problem. It only fails on the local XML documents.

Does anyone know why my XmlReader would fail to parse local XML documents once the application is published?

Here is a small snippet of my XmlReader code for referance:

XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace = true;
            try
            {
                settingsReader = XmlReader.Create("Resources/data/TriviaQuestions.xml", settings);

                nodeNum = 0;
                while (settingsReader.Read())
                {
                    switch (settingsReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (settingsReader.HasAttributes)
                            {
                                for (int i = 0; i < settingsReader.AttributeCount; i++)
                                {
                                    settingsReader.MoveToAttribute(i);
                                    _feeds[nodeNum] = settingsReader.Value.ToString();
                                }
                                settingsReader.MoveToContent(); // Moves the reader back to the element node.
                            }
                            break;
                        case XmlNodeType.Text:
                            _questions[nodeNum] = settingsReader.Value;
                            nodeNum++;
                            break;

                    }

                }
                settingsReader.Close();
            }
            catch
            {

            }

Here is my XML

<?xml version="1.0" encoding="utf-8" ?>
<Questions>
    <Question feed="http://entertainment.msn.com/rss/topboxoffice/"&gt;What movie has the top box office sales in the US right now?</Question>
    <Question feed="http://entertainment.msn.com/rss/topdvdrentals/"&gt;What is the top DVD rental in the US this week?</Question>
    <Question feed="http://entertainment.msn.com/rss/topalbums/"&gt;Which of the following albums is currently topping the charts?</Question>
</Questions>
+1  A: 

Can you describe "but fails once I publish the application and install it" ?

It would be helpful if you could describe what doesn't work, what the error is and provide exception information.


Unrelated to your question, I cringe in horror every time I see empty catch blocks in production code!!

Jeremy Wiebe
+1. for mentioning empty catch blocks!
Mitch Wheat
Empty catch block is because this is a snippet of code, the error handling function call isn't really importatnt in the context of this question.
discorax
+2  A: 

By your publish description I assume you are using clickonce to install the application.

Clickonce has different default behavior for xml files - it assumes they are data files and places them in a different install location from your other files.

Please double check that your xml files really are being installed where you think they are.

In the publish settings you can change the setting for each xml file from data file to include. Your other files will already be set to include.

Note that the publish settings are independent of the build settings for the file.

morechilli
EXACTLY MY PROBLEM! Thank you!
discorax
No worries this caught me out only a couple of days ago.
morechilli
A: 

Consider using IsolatedStorage to store your settings rather than in a relative Resources directory. This will give you a known location for different install scenarios (e.g., ClickOnce installs).

Philipp Schmid
A: 

I have the same problem , i m working on VS 2010 , n i did not know how to apply (morechillie)'s answer , please can u help ?

Developer