2014-01-22 62 views
-1

我創建了一個站點,我有一個支付網關,我想要集成,我需要的是將一些我的c#元素傳遞給Web服務,我需要輸出一個xml文件與我的元素。 請問我該怎麼做?如何使用c#元素輸出xml

需要被髮送的請求如下:

<PaymentRequest> 
<ProductName>Match Ticket</ProductName> (Static value)                                 
<Prefix>Mr</Prefix> (Prefix of the booker)                                                  
<FirstName>Test</FirstName> (First name of the user)                                        
<LastName>Test</LastName> (Last name of the user)                                        
<Address>Test Address</Address> Address of the user(can be a dummy value in case the customer did not fill this)                                       
<City>London</City> City of user(can be a dummy value in case the customer did not fill this)                                                      
<Country>UK</Country> Country of user(can be a dummy value in case the customer did not fill this) 
<Mobile>98231283123</Mobile> (user phone no)                                        
<EmailId>[email protected]</EmailId> (user email)                              
<PaymentMethod>PayatBank</PaymentMethod> (this will be the option selected)                        
<TotalAmount>13314</TotalAmount> (total amount, this is already a label)                                 

<Amount>12926</Amount> (Base amount of the booking)                                          
<GatewayCharge>388</GatewayCharge> (Credit card charges)                                
<GatewayChargeInPercent>3</GatewayChargeInPercent>                
<CallBackURL>http://www.google.com</CallBackURL>                 Link where the user will get redirected after the payment has been made. This will be used for online payments only. 
</PaymentRequest> 
+2

您可以創建一個PaymentRequest類,並使用['XmlSerializer'](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v = vs.110) ).aspx) –

+0

感謝您的答案,需要研究xmlserializer – Kenneth

回答

0

您應該使用XmlSerializer的。你可以使用這個幫手,如果你想:

public static class XmlSerializationHelper 
{ 
    public static string Serialize<T>(T value) 
    { 
     XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); 
     StringWriter writer = new StringWriter(); 
     xmlSerializer.Serialize(writer, value); 

     return writer.ToString(); 
    } 

    public static T Deserialize<T>(string rawValue) 
    { 
     XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); 
     StringReader reader = new StringReader(rawValue); 

     T value = (T)xmlSerializer.Deserialize(reader); 
     return value; 
    } 
} 

也許下次做一些研究。我不認爲這很難找到。

我從這個blog接收它。感謝他。

+0

嗨aloisgd,感謝您的答案,但我怎麼稱呼這在我的單選按鈕行動?還有我如何使用它來在XML中添加我的元素? – Kenneth

+0

您需要創建一個代表「PaymentRequest」的自定義類,並且在上面的代碼中,無論您看到「T」,您都將替換爲您的自定義類。 – dursk

+0

@Kenneth你在用什麼?我猜是WPF?你似乎開始。也許只是在使用這個幫手之前學習一些基礎知識。 – aloisdg