Skip to main content

Microsoft Silverlight

Answered Question My application can't read XMLRSS Feed

(0)

brightstar
brightstar

Member

Member

406 points

184 Posts

My application can't read XML

Hello everybody!

I have a problem with finding XML file to read.

It's a strange error, I had the same error in my Console application as in my Silverlight application.

But in console application I solved it, and in Silverlight I can't repair it. But, I guess, it has the same solution.

I had the next error in my Silverlight:

============================================================================

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)
Timestamp: Mon, 9 Mar 2009 14:53:55 UTC


Message: Unhandled Error in Silverlight 2 Application Cannot find file 'Customers.xml' in the application xap package.
   at System.Xml.XmlXapResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
   at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
   at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
   at System.Xml.XmlReader.Create(String inputUri)
   at xml_tst1.Page.PopulateCustomersList()
   at xml_tst1.Page..ctor()
   at xml_tst1.App.OnStartup(Object sender, StartupEventArgs e)
   at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
Line: 1
Char: 1
Code: 0
URI: http://localhost:49808/Default.html


============================================================================

 

Source of my Silverlight application:

 

Page.xaml.cs

============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;

namespace xml_tst1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            PopulateCustomersList();
        }

        private void PopulateCustomersList()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.XmlResolver = new XmlXapResolver();
            XmlReader reader = XmlReader.Create("Customers.xml");

            reader.MoveToContent();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "customer")
                {
                    customersList.Items.Add(new ListBoxItem()
                    {
                        Content = reader.GetAttribute("last") +
                            ", " + reader.GetAttribute("first") + " (" + reader.ReadInnerXml() + ")"
                    });
                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "customers")
                {
                    break;
                }
            }

            reader.Close();
        }
    }
}

============================================================================

 

Page.xaml

============================================================================ 

<UserControl x:Class="xml_tst1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
          <ListBox x:Name="customersList" Width="400" Height="200" />
    </Grid>
</UserControl>

 ============================================================================

The erros tells, that I don't have XML file. But I have it build in my Silverlight application.

Look!

Here you can see, that Customers.xml exists in my project.

I have the same problem in my Console application. It coudn't find in /Debug folder XML-file. And when I put it, it works normal.

BUT! In error with Silverlight, it teels me, that there isn't XML in my XAP!

But it's in my project, you can see at my first screen!!!

So it's in project ( and after compile ), and why, it isn't built in XAP?

 

Best regards,

BrightStar

fullsailrick
fullsail...

Contributor

Contributor

3699 points

829 Posts

Answered Question

Re: My application can't read XML

Hello Oleg, Make sure that the Build Action on Customers.xml is set to either Content or Resource. You can manipulate the Build Action by right-clicking the XML file in your project, and selecting the Properties option.



Does it work now?

orangechicken
orangech...

Member

Member

15 points

17 Posts

Re: Re: My application can't read XML

 I have the same problem:

 I have an XML file Categories.xml in a folder named Data inside my project. It's Build Action is set to Content. When attempting to load the XML file I reference it absolutely (/Data/Categories.xml), but that throws an exception "Cannot find file '/Data/Categories.xml' in the application xap package".

However, if I rename my .xap to .zip and look inside, sure enough, there's my Categories.xml file in the Data directory. So WTF?

Dave

orangechicken
orangech...

Member

Member

15 points

17 Posts

Re: Re: Re: My application can't read XML

Changing the absolute reference to relative ("/Data/Categories.xml" vs "Data/Categories.xml") fixed it, though that seems buggy since Content is supposed to be relative to the application root - so *both* my references, absolute and relative, should be valid.

Is it actually relative to another root besides the application xap root?

Dave

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities