Send XML to PHP

How can I convert this VB code into C#

Public Function SendXML(sXMLFile As String, fNameValue As String) As MSXML2.ServerXMLHTTP
    On Error Resume Next
    Dim objsrvhttp As New MSXML2.ServerXMLHTTP
    Dim objXMLsend As New MSXML2.DOMDocument
    Dim objXMLReceive As New MSXML2.DOMDocument
    Dim currNode As IXMLDOMNode
    objXMLsend.async = False
   
    If sXMLFile = "" Then End
    objXMLsend.Load (sXMLFile)
   
    Dim fso As New Scripting.FileSystemObject
    fso.CopyFile sXMLFile, "C:\" & fNameValue & ".xml"  
   

    If objXMLsend.parseError.reason <> "" Then
        Print #m_filenum, "XML Error for file: " & fNameValue
        Print #m_filenum, objXMLsend.parseError.reason
        Close m_filenum
        Set SendXML = Null
        Exit Function
    End If
   
    Set currNode = objXMLsend.documentElement.childNodes.item(0)
    objsrvhttp.Open "POST", "url", False
    objsrvhttp.send objXMLsend
    Set objXMLReceive = objsrvhttp.responseXML
   
    Print #m_filenum, objsrvhttp.responseText
    Close m_filenum
    Set SendXML = objsrvhttp
   
End Function

LVL 2
sonashishAsked:
Who is Participating?
 
mkosbieConnect With a Mentor Commented:
currNode and objXMLReceive don't appear to be used for anything in this function.  They go out of scope as soon as the function finishes executing.  Any reason you have them?  Also, I wasn't sure where m_filenum came from, so I made up a filestream for it.  Make sure the stream gets initialized at some point.
using System;
using System.Xml;
using System.IO;
using System.Net;
 
public class XMLExample
{
    private FileStream m_file; //NOTE: You MUST open this FileStream at some point or the function won't work
    private StreamWriter m_filewriter;
 
    public XMLExample()
    {
        //Example opening the streams
        this.m_file = new FileStream("C:\\yourfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        this.m_fileWriter = new StreamWriter(m_file);
    }
 
    public HttpWebRequest SendXML(String sXMLFile, String fNameValue) {
        HttpWebRequest objsrvhttp = WebRequest.Create("url");
        XmlDocument objXMLsend = new XmlDocument();
        XmlDocument objXMLReceive = new XmlDocument();
        XmlNode currNode;
 
        if (sXMLFile = "") return;
        try
        {
            objXMLsend.Load(sXMLFile);
            File.Copy(sXMLFile, "C:\\" + fNameValue + ".xml");
        }
        catch (XmlException ex)
        {
            m_filewriter.WriteLine("XML error for file: " + fNameValue);
            m_filewriter.WriteLine(ex.Message);
            m_filewriter.Close();
            
            return null;
        }finally {}
 
        currNode = objXMLsend.DocumentElement.ChildNodes.Item(0);
        
        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());
        objsrvhttp.Method = "POST";
        objsrvhttp.ContentType = "applicatoin/x-www-form-encoded";
        sw.Write(objXMLsend);
        sw.Close();
        
        objsrvrcv = objsrvhttp.GetResponse();
        StreamReader sr = new StreamReader(objsrvhttp.GetResponse().GetResponseStream());
        String rsp = sr.ReadToEnd();
 
        objXMLReceive.LoadXml(rsp);
        m_filewriter.WriteLine(rsp);
        m_filewriter.Close();
 
        return objsrvhttp;
    }
}

Open in new window

0
 
sonashishAuthor Commented:
I am getting
Cannot implicitly convert type 'System.Net.WebRequest' to 'System.Net.HttpWebRequest'. An explicit conversion exists (are you missing a cast?)       
error on HttpWebRequest objsrvhttp = WebRequest.Create("url"); line.
0
 
mkosbieCommented:
Stupid C#.....  Change the line to be:

HttpWebRequest objsrvhttp = (HttpWebRequest) WebRequest.Create("url");
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sonashishAuthor Commented:
Thanks.
Now I am getting
ProtocolViolationException (Cannot send a content-body with this verb-type.) at
StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream()); line.
0
 
mkosbieCommented:
Sorry, default method for a request is GET.  Switch that line with the line below it (objsrvhttp.Method = "POST") and you should be fine:

        objsrvhttp.Method = "POST";
        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());

INSTEAD of:

        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());
        objsrvhttp.Method = "POST";
0
 
sonashishAuthor Commented:
Now I have a problem in objsrvhttp.GetRequestStream()

objsrvhttp.GetRequestStream()
{System.Net.ConnectStream}
    [System.Net.ConnectStream]: {System.Net.ConnectStream}
    base {System.MarshalByRefObject}: {System.Net.ConnectStream}
    CanRead: false
    CanSeek: false
    CanTimeout: true
    CanWrite: true
    Length: 'objsrvhttp.GetRequestStream().Length' threw an exception of type 'System.NotSupportedException'
    Position: 'objsrvhttp.GetRequestStream().Position' threw an exception of type 'System.NotSupportedException'
    ReadTimeout: 0x000493e0
    WriteTimeout: 0x000493e0

0
 
mkosbieCommented:
Alright, I ran the code through a debugger this time and cleaned it up.  It works fine on my computer (I can instantiate an XMLExample and execute the SendXML method).  I didn't see any changes I made on the objsrvhttp.GetRequestStream() line, but maybe I did.  Try this version and report any errors.
using System;
using System.Xml;
using System.IO;
using System.Net;
 
public class XMLExample
{
    private FileStream m_file; //NOTE: You MUST open this FileStream at some point or the function won't work
    private StreamWriter m_filewriter;
 
    public XMLExample()
    {
        //Example opening the streams
        this.m_file = new FileStream("C:\\yourfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        this.m_filewriter = new StreamWriter(m_file);
    }
 
    public HttpWebRequest SendXML(String sXMLFile, String fNameValue)
    {
        HttpWebRequest objsrvhttp = (HttpWebRequest)WebRequest.Create("URL");
        XmlDocument objXMLsend = new XmlDocument();
        XmlDocument objXMLReceive = new XmlDocument();
        XmlNode currNode;
 
        if (sXMLFile == "") return null;
        try
        {
            objXMLsend.Load(sXMLFile);
            File.Copy(sXMLFile, "C:\\" + fNameValue + ".xml", true);
        }
        catch (XmlException ex)
        {
            m_filewriter.WriteLine("XML error for file: " + fNameValue);
            m_filewriter.WriteLine(ex.Message);
            m_filewriter.Close();
 
            return null;
        }
        catch (Exception ex)
        {
            //All other errors
        }
        finally { }
 
        currNode = objXMLsend.DocumentElement.ChildNodes.Item(0);
 
        objsrvhttp.Method = "POST";
        StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream());
        objsrvhttp.ContentType = "application/x-www-form-encoded";
        sw.Write(objXMLsend);
        sw.Close();
 
        StreamReader sr = new StreamReader(objsrvhttp.GetResponse().GetResponseStream());
        String rsp = sr.ReadToEnd();
 
        try
        {
            objXMLReceive.LoadXml(rsp);
        }
        catch (XmlException ex)
        {
            //Xml response was not in proper format
        }
        catch (Exception ex)
        {
            //All other errors
        }
        finally {}
 
        m_filewriter.WriteLine(rsp);
        m_filewriter.Close();
 
        return objsrvhttp;
    }
}

Open in new window

0
All Courses

From novice to tech pro — start learning today.