2012-08-01 107 views
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; 
} 

控制器的任何幫助將不勝感激。由於

回答

1

Varun的,

[編輯-update]

基於此XML:

<?qbmsxml version="4.5"?> 
<QBMSXML> 
    <SignonMsgsRq> 
    <SignonDesktopRq> 
     <ClientDateTime>2012-07-25T17:13:45</ClientDateTime> 
     <ApplicationLogin>app.app.login.url</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> 

下面是你需要反序列化爲按照示例中的類結構以上:

[XmlRoot("QBMSXML")] 
public class QbmsXml 
{ 
    [XmlElement("SignonMsgsRq")] 
    public SignonMsgsRq SignonMsgsRq { get; set; } 

    [XmlElement("QBMSXMLMsgsRq")] 
    public QbmsXmlMsgsRq QbmsXmlMsgsRq { get; set; } 
} 

public class SignonMsgsRq 
{ 
    [XmlElement("SignonDesktopRq")] 
    public SignonDesktopRq SignonDesktopRq { get; set; } 
} 

public class SignonDesktopRq 
{ 
    [XmlElement("ClientDateTime")] 
    public DateTime ClientDateTime { get; set; } 

    [XmlElement("ApplicationLogin")] 
    public string ApplicationLogin { get; set; } 

    [XmlElement("ConnectionTicket")] 
    public string ConnectionTicket { get; set; } 
} 

public class QbmsXmlMsgsRq 
{ 
    [XmlElement("CustomerCreditCardChargeRq")] 
    public CustomerCreditCardChargeRq CustomerCreditCardChargeRq { get; set; } 
} 

public class CustomerCreditCardChargeRq 
{ 
    [XmlElement("TransRequestID")] 
    public Int64 TransRequestID { get; set; } 

    [XmlElement("CreditCardNumber")] 
    public string CreditCardNumber { get; set; } 

    [XmlElement("ExpirationMonth")] 
    public int ExpirationMonth { get; set; } 

    [XmlElement("ExpirationYear")] 
    public int ExpirationYear { get; set; } 

    [XmlElement("IsCardPresent")] 
    public bool IsCardPresent { get; set; } 

    [XmlElement("Amount")] 
    public double Amount { get; set; } 
} 

只要使用xmlSer ialiser沿線:

class Program 
{ 
    private static T DeSerialize<T>(string fromXmlFile) where T: class 
    { 
     if (!File.Exists(fromXmlFile)) 
     { 
      return default(T); 
     } 
     T deserializedClass; 
     var serializer = new XmlSerializer(typeof(T)); 

     // ToDo: add error catching etc 
     using (var reader = new StreamReader(fromXmlFile)) 
     { 
      deserializedClass = (T)serializer.Deserialize(reader); 
     } 
     return deserializedClass; 
    } 

    static void Main(string[] args) 
    { 
     var yourXmlFilePath = @"d:\temp\xmltest.xml"; 
     var deserializedClass = DeSerialize<QbmsXml>(yourXmlFilePath); 

     Console.WriteLine(deserializedClass 
          .QbmsXmlMsgsRq 
          .CustomerCreditCardChargeRq.Amount); 
     Console.Read(); 
    } 
} 

希望這可以幫助。

+0

它運行但始終返回空,我已確保文件名被提取,並且它加載xml。 – 2012-08-01 10:08:33

+0

public ViewResult Index() var yourXmlFilePath = Server.MapPath(「〜/ XMLData/XMLFile1.xml」); var deserializedClass = DeSerialize (yourXmlFilePath); return View(); } – 2012-08-01 10:09:36

+0

varun,在xml DeSerialize方法中,在使用塊內發生了什麼?另外,請嘗試打開一個簡單的控制檯應用程序,並從我的答案中複製代碼,並查看發生了什麼。最後,你確實意識到你沒有將任何東西傳遞給你的索引視圖。你的返回值應該是'return View(deserializedClass);'並且你的Index.cshtml應該有一個定義爲QbmsXml類型的模型,即'@model yournamespace.Models.QbmsXml' – 2012-08-01 10:22:27