Skip to main content

Microsoft Silverlight

Answered Question Read XML FileRSS Feed

(0)

Tuizi
Tuizi

Member

Member

177 points

90 Posts

Read XML File

Hello,

How I can read a XML File in Silverlight?

 I try:

XmlReader bddXML = XmlReader.Create(new StreamReader("path_to_my_xmlfile));

bddXML.Read();

But I get an Exception :(

The XML File is in my project.

Thank a lot

Fdream
Fdream

Member

Member

86 points

23 Posts

Re: Read XML File

Read it like this:
                Uri requestUri = new Uri("path_to_my_xmlfile", UriKind.Relative);
                HttpWebRequest request = new BrowserHttpWebRequest(requestUri);
                request.Connection = "Close";
                HttpWebResponse response = request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    responseStatus = HttpStatusCode.OK;
                    Stream content = response.GetResponseStream();
                    XmlReader reader = XmlReader.Create(new StreamReader(content, Encoding.UTF8));
                    while (reader.Read())
                    {
                        // your code here
                    }

Related article: How to: Send and Receive Plain XML Requests and Responses over HTTP

=================
http://ooboy.net

Tuizi
Tuizi

Member

Member

177 points

90 Posts

Re: Read XML File

I get an exception on the line:

HttpWebResponse response = request.GetResponse();

Operation is not valid due to the current state of the object.

We are obliged to use an HttpWebRequest to access a local file?

Thank

 

Fdream
Fdream

Member

Member

86 points

23 Posts

Answered Question

Re: Read XML File

For local file, you can try this:

Stream s = this.GetType().Assembly.GetManifestResourceStream("LoadXml.your_xml_file.xml");
using (XmlReader reader = XmlReader.Create(s))
{
   
while (reader.Read())
    {
        //your codes here
    }
}

For more information about GetManifestResourceStream, you can check it in MSDN.

 

=================
http://ooboy.net

Tuizi
Tuizi

Member

Member

177 points

90 Posts

Re: Read XML File

Thank for your answer.

When I do that, s is null, so, the XML File must have a specific characteristic ?

The path must be: LoadXml.my_file.xml or LoadXml.C:\\myproject\\myfile.xml ?

Tuizi
Tuizi

Member

Member

177 points

90 Posts

Answered Question

Re: Read XML File

Lol I'm a newbee... :p

I thought LoadXml was a keyword for GetManifestResourceStream(), so it's the Namespace!!!
 
I change LoadXML by my Namespace and it's work fine!
 
Thanks Big Smile

natarajkt
natarajkt

Member

Member

6 points

4 Posts

Re: Read XML File

Hi.. I tried to read an xml file stored in a location say "c:\". But the GetManifestResourceStream() always returns null.

Cant I read a file external to the project?

my code:

Stream s = this.GetType().Assembly.GetManifestResourceStream("SilverlightProject2.c:\\LiveUpdateLog.xml");using (XmlReader reader = XmlReader.Create(s))

{

 

while (reader.Read())

{

}

}

where SilverlightProject2 is my namespace name.

mario_mh
mario_mh

Member

Member

498 points

116 Posts

Re: Read XML File

hi,

you can't read files from the file system ... this is a security issue.

Mario Meir-Huber
Microsoft Senior Student Partner
Silverlight: www.silverlight-magazin.de

natarajkt
natarajkt

Member

Member

6 points

4 Posts

Re: Re: Read XML File

Hi Mario,

 Iam trying to deploy silverlight application in a cdrom. The silverlight application will be embedded in a HTML page and it will run from a webserver launced from cdrom. Is there any way i can access the images and xml files stored in the cdrom from silverlight application.

 thanks,

nataraj

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities