简体   繁体   English

在C#中传递枚举字段的值以创建xml节点

[英]To Pass Values for Enum Fields in c# to create xml nodes

How do we pass strings to enum fields of a cs file (cs file of xsd file). 我们如何将字符串传递给CS文件(XSD文件的CS文件)的枚举字段。

I have written the following code, 我写了下面的代码,

string PATH = "C:\\Sample.xml";
CreateEmptyFile(PATH);

var data = new AutoCount();
data.Product = "AutoCount Accounting";
data.Version = "1.5";
data.CreatedApplication = "BApp";
data.CreatedBy = "Business Solutions";
data.CreatedDateTime = DateTime.Today;
data.CreatedDateTimeSpecified = true;
var detail = new SalesIVCSDetailNode();
detail.ItemsElementName = new [] {
    ItemsChoiceType3.ItemCode,
    ItemsChoiceType3.UOM,
    ItemsChoiceType3.Qty,
    ItemsChoiceType3.UnitPrice
};

/* above line */
detail.TaxType = "SR";

List<SalesInvoice> salesInvoices = new List<SalesInvoice>();

for (int i = 0; i < dataGridView1.RowCount - 1; i++) {
    var salesInvoice = new SalesInvoice();
    salesInvoice.DocNo = dataGridView1.Rows[i].Cells[0].FormattedValue.ToString();
    salesInvoice.DocDate = DateTime.Today;
    salesInvoice.DebtorCode = "ABC Company";
    salesInvoice.Detail =new [] {detail};
    salesInvoices.Add(salesInvoice);
}

data.SalesInvoice = salesInvoices.ToArray();
var serializer = new XmlSerializer(typeof(AutoCount));
using (var stream = new StreamWriter(PATH))
serializer.Serialize(stream, data);

I want the output to be: 我希望输出为:

<?xml version="1.0" encoding="utf-8" ?>
<AutoCount xmlns="http://schemas.autocountsoft.com/ac_accounting.xsd">
<Product>AutoCount Accounting</Product>
<Version>1.5</Version>
<CreatedApplication>BApp</CreatedApplication>
<CreatedDateTime>2015-02-23T09:51:54.5746</CreatedDateTime>
<CreatedBy>Business Solutions</CreatedBy>
<SalesInvoice DocNo="IV-0022" ImportAction="AddUpdate">
  <DocDate>2015-02-15</DocDate>
  <DebtorCode>I50201</DebtorCode>
  <Detail>
    <ItemCode>IMP001</ItemCode>
    <UOM>PCS</UOM>
    <Qty>2</Qty>
    <UnitPrice>15.00</UnitPrice>
  </Detail>
</SalesInvoice>
</AutoCount>

Problem is i am not aware of how to send strings for enum fields. 问题是我不知道如何为枚举字段发送字符串。 i dono how to create nodes .How do we do it? 我不如何创建节点。我们该怎么做?

the line in the c# query.'ItemsChoiceType3' is enum. c#查询中的行。“ ItemsChoiceType3”是枚举。

detail.ItemsElementName = new [] {
    ItemsChoiceType3.ItemCode,
    ItemsChoiceType3.UOM,
    ItemsChoiceType3.Qty,
    ItemsChoiceType3.UnitPrice
};

EDIT: The Following is the autocount.cs file taken from autocount.xsd file. 编辑:以下是从autocount.xsd文件中提取的autocount.cs文件。 I need help in accessing the itemcode,qty,uom,unitprice tags and passing values to it (hardcoding or database) 在访问itemcode,qty,uom,unitprice标签并将值传递给它(硬编码或数据库)时,我需要帮助

public partial class SalesIVCSDetailNode
 {
  private object[] itemsField;
  private ItemsChoiceType3[] itemsElementNameField;
  private string locationField;
  private string descriptionField;
  [System.Xml.Serialization.XmlElementAttribute("ItemCode", typeof(string))]
  [System.Xml.Serialization.XmlElementAttribute("PackageCode", typeof(string))]
  [System.Xml.Serialization.XmlElementAttribute("Qty", typeof(decimal))]
  [System.Xml.Serialization.XmlElementAttribute("UOM", typeof(string))]
  [System.Xml.Serialization.XmlElementAttribute("UnitPrice", typeof(decimal))]
         [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
  public object[] Items
  {
     get
     {
        return this.itemsField;
     }
     set
     {
        this.itemsField = value;
     }
  }
  [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
  [System.Xml.Serialization.XmlIgnoreAttribute()]      
  public ItemsChoiceType3[] ItemsElementName
  {
     get
     {
        return this.itemsElementNameField;
     }
     set
     {
        this.itemsElementNameField = value;
     }
  }    
}


public enum ItemsChoiceType3
{

  ItemCode,

  PackageCode,

  Qty,

  UOM,

  UnitPrice,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM