problem accessing xml file via stream

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hey

..NET 2.0

In my code am I trying to access an xml file using streaming. My code
compile but I think there is some kind of error in my code...:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create( <url to xml
file> );
request.Credentials = CredendtialCache.DefaultCredentials;
request.Accept = "text/xml";

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();

XmlDocument doc = new XmlDocument();
XmlReader reader = XmlReader.Create(stream);
doc.Load(reader); //<???
XmlElement element = doc.DocumentElement["helloworld"];

I'm thinking if it might be wrong to use Load (see <??? in code) as this is
a stream. maybe Load does not load the entire xml file...

any ideas
 
In my code am I trying to access an xml file using streaming. My code
compile but I think there is some kind of error in my code...:

You haven't said what happens when you try your code as it is. You're
certainly missing "using" statements around stuff, but apart from that
it looks okay to me at first sight. What happens when you run it?

Jon
 
Jeff said:
Hey

.NET 2.0

In my code am I trying to access an xml file using streaming. My code
compile but I think there is some kind of error in my code...:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create( <url to xml
file> );
request.Credentials = CredendtialCache.DefaultCredentials;
request.Accept = "text/xml";

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();

XmlDocument doc = new XmlDocument();
XmlReader reader = XmlReader.Create(stream);
doc.Load(reader); //<???
XmlElement element = doc.DocumentElement["helloworld"];

I'm thinking if it might be wrong to use Load (see <??? in code) as this is
a stream. maybe Load does not load the entire xml file...

any ideas

When asking for help, always provide all the clues necessary. With your
above code and, in particular, comments, you're forcing someone that
wants to help you to copy and paste the above code into a full program,
create an xml file, and test it to figure out why you think something is
wrong.

Why do you think something is wrong? What is the symptoms of this
"wrongness" you need help with?
 
the problem is that I'm trying to access a specific element at :
XmlElement element = doc.DocumentElement["helloworld"];
and because doc is based on stream I don't think that element is read in
(reader.Read())

And also when I'm debugging this code this line:
XmlReader reader = XmlReader.Create(stream);
gives reader the value {none} in the debugger, which made me think either
this document contain none element or it gives none because none elements
are read in from the stream...

(sorry I'm a bit newbie on xml stuff)
 
While debugging the code I've discovered that this line:
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
gives response.ContentLength the value -1

I think that is the reason I get {none} later in the code
 
the problem is that I'm trying to access a specific element at :
XmlElement element = doc.DocumentElement["helloworld"];

That's not a problem - that's what you're trying to do. What happens
when you do it? Do you get an exception?
and because doc is based on stream I don't think that element is read in
(reader.Read())

You're not calling reader.Read() though - at least in the code you
showed us - and indeed you shouldn't. XmlDocument.Load() will be
calling Read() repeatedly.
And also when I'm debugging this code this line:
XmlReader reader = XmlReader.Create(stream);
gives reader the value {none} in the debugger, which made me think either
this document contain none element or it gives none because none elements
are read in from the stream...

Hmm... I'd have to fire up a debugger to see if I could reproduce
that. However, similar sample code I've got here works fine.
(sorry I'm a bit newbie on xml stuff)

What happens if you look at doc.OuterXml in the debugger, after
calling doc.Load()?

Jon
 
While debugging the code I've discovered that this line:
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
gives response.ContentLength the value -1

I think that is the reason I get {none} later in the code

Not necessarily. That's what you'd see if the content-length header
just wasn't specified. It doesn't mean there's no data.

Jon
 
Jeff said:
HttpWebRequest request = (HttpWebRequest) WebRequest.Create( <url to xml
file> );
request.Credentials = CredendtialCache.DefaultCredentials;
request.Accept = "text/xml";

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();

XmlDocument doc = new XmlDocument();
XmlReader reader = XmlReader.Create(stream);
doc.Load(reader); //<???
XmlElement element = doc.DocumentElement["helloworld"];

I'm thinking if it might be wrong to use Load (see <??? in code) as this is
a stream. maybe Load does not load the entire xml file...

I don't see any problem with your code although usually you do not need
to create an XmlReader explicitly, you can simply pass a stream to the
Load method e.g.
doc.Load(stream);
If the last line (XmlElement element =
doc.DocumentElement["helloworld"];) does not find an element then
perhaps the XML does not have the structure you expect. You will have to
show us the XML to enable us to judge whether that expression should
find an element.
 
Okay, I think the problem is in the URL..

I'm trying to access a URL via which gives my an "access denied"... It
returns an xml document but it doesn't return the expected values. Instead
doc.InnerXml get this value:
<response><error>access_denied</error></response>

The problem isn't in my code, but in the URL instead. So consider this for
being solved..
 
Hi,

Not really, basically it says that it does not know how big the response
will be
 
Hi,

Take this as a lesson to try everything before posting a problem
 
Back
Top