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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mkosbieCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.