如何使用 XDocument 编写子元素

How to write the Child Elements using XDocument

我已经修改了问题并包含了到目前为止我为这个问题编写的代码。下面是一个输出必须看起来像与 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已经制作了一些与设备通信所需要的方法。所以下面的代码有方法调用和class级变量,写了但没有包含在下面的例子中:

/// <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;


        }

如果有人能帮助我理解如何按照我在代码中放置注释的位置填充子元素,我将非常感激。

感谢版主要求提供更多相关信息。

你可以使用 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);

我认为使用 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; }
}

结果: