problem accessing xml file via stream

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
 
J

Jon Skeet [C# MVP]

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
 
L

Lasse Vågsæther Karlsen

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?
 
J

Jeff

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)
 
J

Jeff

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
 
J

Jon Skeet [C# MVP]

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
 
J

Jon Skeet [C# MVP]

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
 
M

Martin Honnen

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.
 
J

Jeff

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..
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top