1

I'm using C# with Razor and a co-worker supplied me with a VB.net Class example of how he queries a web service for XML data. I'm trying to convert this to C# and I'm having issues trying to parse the XML data. I receive an error in VS2015 (see below the C# code)

Any Help is greatly Appreciated!!

NOTE: The XML data produced by the web service ALWAYS contains only ONE node (or element) called "string"

EDIT This is not a duplicate of the NULLReferenceError. I ask that you please read my issue first before trying to assume a duplicate.

VB.net Class (His working version)

Imports System.Net
Imports System.IO

Public Class Main
   Inherits System.Web.UI.Page
   Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        TextBox1.Text = ""
        Dim URLString As String = ***THE WORKING URL STRING TO RETRIEVE XML DATA GIVEN PARAMETERS ***

        Dim XMLdoc As XDocument = Nothing
        Dim Headers As WebHeaderCollection = New WebHeaderCollection
        Dim OCon As HttpWebRequest = CType(WebRequest.Create(URLString), HttpWebRequest)

        OCon.Method = "GET"
        OCon.ContentType = "application/x-www-form-urlencoded"
        OCon.Headers = Headers

        Dim ApiResponse As HttpWebResponse = CType(OCon.GetResponse, HttpWebResponse)
        Dim Reader As StreamReader = New StreamReader(ApiResponse.GetResponseStream)
        Dim XMLResponse As String = Reader.ReadToEnd

        Reader.Close()
        ApiResponse.Close()

        XMLdoc = XDocument.Parse(XMLResponse)
        TextBox1.Text = XMLdoc.ToString
    End Sub
End Class

C# (using Razor - My attempt to Convert)

@using System.Net;
@using System.IO;
@using System.Xml;
@{        
    string URLString = *** SAME URL AS ABOVE ***;

    XmlDocument XMLdoc = new XmlDocument();
    HttpWebRequest OCon = (HttpWebRequest)WebRequest.Create(URLString);
    WebHeaderCollection Headers = new WebHeaderCollection();

    OCon.Method = "GET";
    OCon.ContentType = "application/x-www-form-urlencoded";
    OCon.Headers = Headers;

    HttpWebResponse ApiResponse = (HttpWebResponse)OCon.GetResponse();
    StreamReader Reader = new StreamReader(ApiResponse.GetResponseStream());
    string XMLResponse = Reader.ReadToEnd();

    Reader.Close();
    ApiResponse.Close();

    //This is where I have issues. I can't figure out how to parse the one node.
    XmlNodeList nodes = XMLdoc.DocumentElement.SelectNodes("/string");
    foreach (XmlNode node in nodes)
    {
        <p>@node.ToString()</p>
    }
}

The Error I get in VS2015 with my code is the following:

NullReferenceException was unhandled by user code:

An exception of type 'System.NullReferenceException' occurred in App_Web_a14yc3eh.dll but was not handled in user code

Additional Information: Object reference not set to an instance of an object.

An example of what the XML output from the URL looks like

enter image description here

bagofmilk
  • 1,492
  • 8
  • 34
  • 69
  • 3
    `XMLdoc` is just floating about, you never assign it any XML, if its than simple just `var content = XElement.Parse(XMLResponse).Value;` – Alex K. Jul 07 '17 at 16:18
  • That's it, Alex. I needed to use `XElement`. Thank you very much – bagofmilk Jul 07 '17 at 16:24

0 Answers0