1
我想實現一個支付網關(INTUIT支付網關)。我想序列化一個XML到模型類並保存到我的數據庫。我使用桌面模型直觀支付網關,因爲託管模型是一個特別是SSL證書工作的痛苦,所以我不想嘗試。XML文件模型映射asp.net MVC3(動態地將一個xml文件映射到一個模型類asp.net mvc3)
我必須提及,我能夠使用下面提到的代碼獲得響應,在那裏我卡住的是序列化xml並將響應保存到數據庫中。這個XML是從我的項目中位於我的xmlfiles文件夾中的文件夾中提取的。
有關示例xml格式,請查看這一個。
這是一個XML文件,我試圖張貼到URL(https://merchantaccount.ptc.quickbooks.com/j/AppGateway)
<?xml version="1.0"?>
<?qbmsxml version="4.5"?>
<QBMSXML>
<SignonMsgsRq>
<SignonDesktopRq>
<ClientDateTime>2012-07-25T17:13:45</ClientDateTime>
<ApplicationLogin>abc.abc.us</ApplicationLogin>
<ConnectionTicket>TGT-1-g42FGaMfOTQ82GcWFBpsuQ</ConnectionTicket>
</SignonDesktopRq>
</SignonMsgsRq>
<QBMSXMLMsgsRq>
<CustomerCreditCardChargeRq>
<TransRequestID>4540453787200</TransRequestID>
<CreditCardNumber>4111111111111111</CreditCardNumber>
<ExpirationMonth>12</ExpirationMonth>
<ExpirationYear>2016</ExpirationYear>
<IsCardPresent>false</IsCardPresent>
<Amount>10.00</Amount>
</CustomerCreditCardChargeRq>
</QBMSXMLMsgsRq>
</QBMSXML>
我用它來發表的帖子的網址
public ActionResult Index()
{
WebRequest req = null;
WebResponse rsp = null;
string fileName = Server.MapPath("~/XMLData/XMLFile1.xml");
string uri = "https://merchantaccount.ptc.quickbooks.com/j/AppGateway";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST"; // Post method
req.ContentType = "application/x-qbmsxml"; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(this.GetTextFromXMLFile(fileName));
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
// var resp = (Object)rsp.GetResponseStream();
if (rsp != null)
{
StreamReader inStream = new StreamReader(rsp.GetResponseStream());
var data = inStream.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(data);
string path = Server.MapPath("~/XMLData/SampleXmlE2E.xml");
xmlDoc.Save(path);//regenerates the xml file in different system.
}
return View();
//XElement root = new XElement("root");
//root.Add(new XElement("element1"));
//root.Add(new XElement("element2"));
//root.Add(new XAttribute("attribute1", "a value"));
//return new XmlResult(root);
}
private string GetTextFromXMLFile(string file)
{
StreamReader reader = new StreamReader(file);
string ret = reader.ReadToEnd();
reader.Close();
return ret;
}
private string SendRequest(Uri UriObj, string data)
{
string _result;
var request = (HttpWebRequest)WebRequest.Create(UriObj);
request.Method = "POST";
request.ContentType = "text/xml";
var writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
var response = (HttpWebResponse)request.GetResponse();
var streamResponse = response.GetResponseStream();
var streamRead = new StreamReader(streamResponse);
_result = streamRead.ReadToEnd().Trim();
streamRead.Close();
streamResponse.Close();
response.Close();
return _result;
}
控制器的任何幫助將不勝感激。由於
它運行但始終返回空,我已確保文件名被提取,並且它加載xml。 – 2012-08-01 10:08:33
public ViewResult Index() var yourXmlFilePath = Server.MapPath(「〜/ XMLData/XMLFile1.xml」); var deserializedClass = DeSerialize(yourXmlFilePath); return View(); } –
2012-08-01 10:09:36
varun,在xml DeSerialize方法中,在使用塊內發生了什麼?另外,請嘗試打開一個簡單的控制檯應用程序,並從我的答案中複製代碼,並查看發生了什麼。最後,你確實意識到你沒有將任何東西傳遞給你的索引視圖。你的返回值應該是'return View(deserializedClass);'並且你的Index.cshtml應該有一個定義爲QbmsXml類型的模型,即'@model yournamespace.Models.QbmsXml' – 2012-08-01 10:22:27