[英]To Pass Values for Enum Fields in c# to create xml nodes
我們如何將字符串傳遞給CS文件(XSD文件的CS文件)的枚舉字段。
我寫了下面的代碼,
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);
我希望輸出為:
<?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>
問題是我不知道如何為枚舉字段發送字符串。 我不如何創建節點。我們該怎么做?
c#查詢中的行。“ ItemsChoiceType3”是枚舉。
detail.ItemsElementName = new [] {
ItemsChoiceType3.ItemCode,
ItemsChoiceType3.UOM,
ItemsChoiceType3.Qty,
ItemsChoiceType3.UnitPrice
};
編輯:以下是從autocount.xsd文件中提取的autocount.cs文件。 在訪問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,
}
嘗試用XMLEnumAttribute裝飾- https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.