CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2012
    Posts
    2

    XMLReader not working

    Hello!

    I'm playing around trying to read XML-files from the Internet to parse, but I can't get it to work at all. Some code:

    try {
    HttpWebRequest^ req = dynamic_cast<HttpWebRequest^> (WebRequest::Create( "http://www.xmlfiles.com/examples/note.xml" ));
    HttpWebResponse^ res = dynamic_cast<HttpWebResponse^> (req->GetResponse());


    Stream^ stm = res->GetResponseStream();
    XmlTextReader^ reader;
    try {reader = dynamic_cast<XmlTextReader^>(XmlTextReader::Create(stm)); }
    catch (...){Console::WriteLine("b");}

    while (reader->Read()){
    switch (reader->NodeType){
    default:
    Console::WriteLine("a");
    // Console::WriteLine(String::Concat( reader->NodeType.ToString(), ": ","<", reader->Name, ">",reader->Value));
    break;
    }
    }
    }
    catch (Exception^ e){
    Console::WriteLine(e->StackTrace);
    Console::WriteLine(e->Message);
    }

    All I get is an error: "Object reference not set to an instance of an object. The stacktrace points at the Read()-operation.

    Anyone know about how to solve this?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: XMLReader not working

    Quote Originally Posted by SamiH View Post
    All I get is an error: "Object reference not set to an instance of an object. The stacktrace points at the Read()-operation.
    Unless this exception gets thrown as a result of an internal error in XmlTextReader::Read(), which I consider to be rather unlikely, it seems to indicate either of two things: XmlTextReader::Create() returned nullptr or it returned something that couldn't be legally cast into an XmlTextReader ^. (If you had used a safe_cast instead of the dynamic_cast, these two scenarios would have led to different exceptions. That's one of the things I really learned to appreciate about the safe_cast.) Given the name of the static method and the class it's a member of, the latter seems to be rather unlikely, and as of now I didn't research any potential reasons for the former. Whether reader actually is nullptr at the point where the exception gets thrown can easily be verified by debugging.

    BTW:

    Code:
    			//	Console::WriteLine(String::Concat( reader->NodeType.ToString(), ": ","<", reader->Name, ">",reader->Value));
    Though commented out, this statement can be written in a much more readable way (IMO, not commented out here):

    Code:
    Console::WriteLine("{0}: <{1}>{2}", reader->NodeType, reader->Name, reader->Value);
    Console::Writeline() can do a bit more than just line up strings it gets passed as parameters. The statement above uses this overload of Console::WriteLine(): http://msdn.microsoft.com/en-us/library/a0bfz20d.aspx.

    Please use code tags when posting code.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Re: XMLReader not working

    Thanks for the reply!

    I broke down the errorhandling, and found out that it was the cast from XmlTextReader::Create(stm) that did not return an XmlTextReader, but another class inheriting from XmlReader. Just assigning it to a XmlReader pointer solved the problem!

    Kind of off-topic, but since you seem to be a wiz, would you have any idea about this problem:

    The program above works if I input an address to a XML-file. But if the address is to a server, which would have returned a XML-file if I pasted it into a web browser, this doesn't seem to work. For example:

    In a web browser

    http://api.arbetsformedlingen.se/pla...a/yrkesomraden

    returns an XML-file.

    In my program it returns (400) Bad Request.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: XMLReader not working

    Quote Originally Posted by SamiH View Post
    I broke down the errorhandling, and found out that it was the cast from XmlTextReader::Create(stm) that did not return an XmlTextReader, but another class inheriting from XmlReader. Just assigning it to a XmlReader pointer solved the problem!
    Ah, I see. Checking that on MSDN I found that XmlTextReader doesn't even have a Create() method, it just inherits one (or more exact: a dozen overloads of it) from XmlReader, and AFAICT (only had a closer look on the one you were calling) they all return an XmlReader that, despite the inheritance relation, can't be cast into an XmlTextReader. (A cast in the other direction would work, however.)

    [...] But if the address is to a server, which would have returned a XML-file if I pasted it into a web browser, this doesn't seem to work. For example:

    In a web browser

    http://api.arbetsformedlingen.se/pla...a/yrkesomraden

    returns an XML-file.

    In my program it returns (400) Bad Request.
    To me that looks like an HTTP protocol error, and unfortunately that's an area I'm not a wiz on. At any rate, at least my IE8 (executed as secure browser under Kaspersky Security Suite) isn't able to open that URL either.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured