Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中传递枚举字段的值以创建xml节点_C#_Xml_Enums_Xsd - Fatal编程技术网

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

在c#中传递枚举字段的值以创建xml节点,c#,xml,enums,xsd,C#,Xml,Enums,Xsd,如何将字符串传递给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 So

如何将字符串传递给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);
编辑: 以下是取自autocount.xsd文件的autocount.cs文件。 我需要帮助访问itemcode、qty、uom、单价标签并向其传递值(硬编码或数据库)


尝试用XMLEnumAttribute来装饰-

您能给出一个如何发送字符串的示例吗?公共枚举项schoicetype3{[XmlEnum(Name=“IMP001”)]ItemCode[XmlEnum(Name=“PCS”)]UOM[XmlEnum(Name=“2”)]Qty[XmlEnum(Name=“15.00”)]UnitPrice}但是更好的方法是创建新类,比如:类ItemsElementName{public ItemsChoiceType3 Type{get;set;}公共字符串描述{get;set;}如何在上面的c代码中调用它以获得预期的结果。?我在从xsd文件生成的cs文件中有enum类。我应该直接更改cs文件吗?如果是这样的话,我们如何在xml文件中获取值?重复。给出了解决方案,一式两份。请仔细阅读问题的答案。你试过我的代码了吗?我使用WriteSXML方法和WriteSchema选项来保存嵌入XML文件中的xsd数据。
<?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>
detail.ItemsElementName = new [] {
    ItemsChoiceType3.ItemCode,
    ItemsChoiceType3.UOM,
    ItemsChoiceType3.Qty,
    ItemsChoiceType3.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,
}