2012-08-01 78 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