Solved

Send XML to PHP

Posted on 2008-10-20
7
391 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:sonashish
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
mkosbie earned 500 total points
ID: 22760348
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
 
LVL 2

Author Comment

by:sonashish
ID: 22760578
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
 
LVL 8

Expert Comment

by:mkosbie
ID: 22760594
Stupid C#.....  Change the line to be:

HttpWebRequest objsrvhttp = (HttpWebRequest) WebRequest.Create("url");
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Author Comment

by:sonashish
ID: 22760679
Thanks.
Now I am getting
ProtocolViolationException (Cannot send a content-body with this verb-type.) at
StreamWriter sw = new StreamWriter(objsrvhttp.GetRequestStream()); line.
0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22760826
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
 
LVL 2

Author Comment

by:sonashish
ID: 22762816
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
 
LVL 8

Expert Comment

by:mkosbie
ID: 22785663
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question