?
Solved

Send XML to PHP

Posted on 2008-10-20
7
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
mkosbie earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

718 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