Skip to main content
Home Forums Silverlight Programming Programming with .NET - General My application can't read XML
3 replies. Latest Post by orangechicken on March 11, 2009.
(0)
brightstar
Member
406 points
184 Posts
03-09-2009 11:11 AM |
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 detailsUser 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 UTCMessage: 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: 1Char: 1Code: 0URI: 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
fullsail...
Contributor
3699 points
829 Posts
03-10-2009 3:56 AM |
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?
orangech...
15 points
17 Posts
03-11-2009 1:55 PM |
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
03-11-2009 1:58 PM |
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?