我已修改了这个问题并包含了我迄今为止为此问题编写的代码。下面是输出必须与VeriFone MX915支付终端兼容的示例。在这个特定的部分,我试图将POS注册项目发送到显示器。
<TRANSACTION>
<FUNCTION_TYPE>LINE_ITEM</FUNCTION_TYPE>
<COMMAND>ADD</COMMAND>
<COUNTER>1</COUNTER>
<MAC> … </MAC>
<MAC_LABEL>REG2</MAC_LABEL>
<RUNNING_SUB_TOTAL>7.00</RUNNING_SUB_TOTAL>
<RUNNING_TRANS_AMOUNT>7.42</RUNNING_TRANS_AMOUNT>
<RUNNING_TAX_AMOUNT>0.42</RUNNING_TAX_AMOUNT>
<LINE_ITEMS>
<MERCHANDISE>
<UNIT_PRICE>5.00</UNIT_PRICE>
<DESCRIPTION>#1 Combo Meal</DESCRIPTION>
<LINE_ITEM_ID>1695155651</LINE_ITEM_ID>
<EXTENDED_PRICE>5.00</EXTENDED_PRICE>
<QUANTITY>1</QUANTITY>
</MERCHANDISE>
</LINE_ITEMS>
</TRANSACTION>
VeriFone提供的SDK提供了与设备通信所需的一些方法。因此,以下代码具有编写但未包含在以下示例中的方法调用和类级别变量:
/// <summary>
/// Send 1 or more items to the Verifone display. Include subtotal, tax and total
/// </summary>
/// <param name="nSubTotal">Subtotal of transaction</param>
/// <param name="nTax">Current Tax of transaction</param>
/// <param name="nTotal">Total of transaction</param>
/// <param name="UPC">Item Code</param>
/// <param name="ShortDescription">Small description</param>
/// <param name="nItemAmount">Item Amt</param>
/// <param name="nQty">Quantity</param>
/// <param name="nExtendedAmount">Quantity X Item Amt</param>
/// <returns></returns>
public bool AddLineItem(double nSubTotal, double nTax, double nTotal, Int32 nItemID, string UPC, string ShortDescription, double nItemAmount, Int32 nQty, double nExtendedAmount)
{
// get counter and calculate Mac
var nextCounter = (++counter).ToString();
var mac = PrintMacAsBase64(macKey, nextCounter);
// build request
var request = new XDocument();
using (var writer = request.CreateWriter())
{
//populate the elements
writer.WriteStartDocument();
writer.WriteStartElement("TRANSACTION");
writer.WriteElementString("FUNCTION_TYPE", "LINE_ITEM");
writer.WriteElementString("COMMAND", "ADD");
writer.WriteElementString("MAC_LABEL", macLabel);
writer.WriteElementString("COUNTER", nextCounter);
writer.WriteElementString("MAC", mac);
writer.WriteElementString("RUNNING_SUB_TOTAL",nSubTotal.ToString("c"));
writer.WriteElementString("RUNNING_TAX_AMOUNT",nTax.ToString("c"));
writer.WriteElementString("RUNNING_TRANS_AMOUNT",nTotal.ToString("c"));
//HERE IS WHERE I NEED TO WRITE THE CHILD ELEMENT(s):
//example below of what they or it would look like
//(collection of merchandise elements under Line_items)
//I know this example will have only one item because of parameters
/*
<LINE_ITEMS>
<MERCHANDISE>
<LINE_ITEM_ID>10</LINE_ITEM_ID>
<DESCRIPTION>This is a dummy</DESCRIPTION>
<UNIT_PRICE>1.00</UNIT_PRICE>
<QUANTITY>1</QUANTITY>
<EXTENDED_PRICE>1.00</EXTENDED_PRICE>
</MERCHANDISE>
</LINE_ITEMS>
*/
writer.WriteEndElement();
writer.WriteEndDocument();
}
// transmit to Point Solution and interrogate the response
var responseXml = Send(address, port, request);
//DO SOMETHING HERE WITH THE RESPONSE
// validate that the RESULT_CODE came back a SUCCESS
if ("-1" != responseXml.Element("RESPONSE").Element("RESULT_CODE").Value)
{
throw new Exception(responseXml.Element("RESPONSE").Element("RESULT_TEXT").Value ?? "unknown error");
}
return true;
}
如果任何人可以帮助我理解如何填充子元素,如我在代码中放置注释所示,我将非常感激。
感谢主持人要求提供更多相关信息。
答案 0 :(得分:1)
我认为使用XmlSerializer是一个好主意,下面的代码可能对你有帮助。 XmlSerializer比XmlWriter更直观地生成Xml。 :)
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
class Program
{
static void Main(string[] args)
{
Transaction transaction = new Transaction();
transaction.Function_Type = "LINE_ITEM";
transaction.LineItems = new List<Merchandise>();
transaction.LineItems.Add(new Merchandise() { UnitPrice = "5.00" });
//Create our own namespaces for the output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
//Add an empty namespace and empty value
ns.Add("", "");
using (XmlWriter writer = XmlWriter.Create(Console.Out, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
new XmlSerializer(typeof(Transaction)).Serialize(writer, transaction, ns);
}
Console.Read();
}
}
[XmlRoot("TRANSACTION")]
public class Transaction
{
[XmlElement("FUNCTION_TYPE")]
public string Function_Type { get; set; }
[XmlArray("LINE_ITEMS")]
[XmlArrayItem("MERCHANDISE")]
public List<Merchandise> LineItems { get; set; }
}
[XmlRoot("MERCHANDISE")]
public class Merchandise
{
[XmlElement("UNIT_PRICE")]
public string UnitPrice { get; set; }
}
答案 1 :(得分:0)
您可以使用XmlDocument
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("RootNode");
xmlDoc.AppendChild(rootNode);
foreach (Class objItem in classArray)
{
XmlNode firstNode= xmlDoc.CreateElement("First");
XmlNode second= xmlDoc.CreateElement("Second");
second.InnerText = objItem .Text;
firstNode.AppendChild(second);
rootNode.AppendChild(firstNode);
}
StringWriter stringWriter = new StringWriter();
XmlTextWriter textWriter = new XmlTextWriter(stringWriter);
xmlDoc.WriteTo(textWriter);