【发布时间】:2019-08-27 14:52:06
【问题描述】:
我是编程新手,尤其是 XML 和 XSD,我需要帮助。
我有一个 XSD 文件,你可以在这里找到 InvoicesDoc-v0.5.xsd
我用 Xsd2Code 和命令转换它
xsd InvoicesDoc-v0.5.xsd /classes
进入类,然后我尝试基于该 XSD 创建一个 XML 字符串。 我创建 XML 的代码是
public partial class MyDataOperations
{
private Timologio TimologioCurrent { get; set; }
private StringBuilder ErrorBuilder { get; set; }
private AadeBookInvoiceType aadeBookInvoiceType { get; set; }
private InvoicesDoc invoicesDoc { get; set; }
public MyDataOperations(Timologio timologio)
{
TimologioCurrent = timologio;
invoicesDoc = new InvoicesDoc();
LengthWarn.WarnBuilder = new StringBuilder();
}
public void Create()
{
Header();
Ekdotis();
Paraliptis();
Epikefalida();
Grammes();
Perilipsi();
}
void Header()
{
aadeBookInvoiceType= new AadeBookInvoiceType();
}
void Ekdotis()
{
PartyType partyType = new PartyType();
partyType.vatNumber = TimologioCurrent.Diasafistis.Afm;
partyType.country = CountryType.GR;
aadeBookInvoiceType.issuer = partyType;
}
void Paraliptis()
{
PartyType partyType = new PartyType();
partyType.vatNumber = TimologioCurrent.Afm;
partyType.country = CountryType.GR;
aadeBookInvoiceType.counterpart = partyType;
}
void Epikefalida()
{
InvoiceHeaderType HeaderType = new InvoiceHeaderType();
HeaderType.aa = '1';
HeaderType.issueDate = DateTime.Today;
}
void Grammes()
{
InvoiceRowType rowType = new InvoiceRowType();
rowType.lineNumber = '1';
rowType.invoiceDetailTypeSpecified = true;
rowType.invoiceDetailType = '1';
InvoiceRowTypeNetValue value = new InvoiceRowTypeNetValue();
value.Value = 200;
rowType.vatCategory = 3;
}
void Perilipsi()
{
InvoiceSummaryType summaryType = new InvoiceSummaryType();
summaryType.totalNetValue = 200;
summaryType.totalVatAmount = 100;
summaryType.totalWithheldAmount = 0;
summaryType.totalFeesAmount = 0;
summaryType.totalStampDutyAmount = 0;
summaryType.totalOtherTaxesAmount = 0;
summaryType.totalDeductionsAmount = 0;
summaryType.totalGrossValue = 0;
}
public override string ToString()
{
return GetStringMessage();
}
public string GetStringMessage()
{
return aadeBookInvoiceType.Serialize();
}
}
我的问题是,当我创建要发送的 XML 字符串时,它的格式错误。 我创建的 XML 具有以下格式
<?xml version="1.0" encoding="utf-8"?>
<AadeBookInvoiceType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.aade.gr/myDATA/invoice/v1.0">
<issuer>
<vatNumber>043529374</vatNumber>
<country>GR</country>
<address>Thessaloniki</address>
</issuer>
<counterpart>
<vatNumber>094006956</vatNumber>
<country>GR</country>
<address>Thessaloniki</address>
</counterpart>
<invoiceHeader>
<branch>0</branch>
<aa>1</aa>
<issueDate>2019-08-05</issueDate>
<invoiceType>1.1</invoiceType>
<currency>EUR</currency>
</invoiceHeader>
<invoiceSummary>
<totalNetValue>0</totalNetValue>
<totalVatAmount>0</totalVatAmount>
<totalWithheldAmount>0</totalWithheldAmount>
<totalFeesAmount>0</totalFeesAmount>
<totalStampDutyAmount>0</totalStampDutyAmount>
<totalOtherTaxesAmount>0</totalOtherTaxesAmount>
<totalDeductionsAmount>0</totalDeductionsAmount>
<totalGrossValue>0</totalGrossValue>
</invoiceSummary>
</AadeBookInvoiceType>
并且我希望得到以下格式的输出
<?xml version="1.0" encoding="utf-8"?>
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.aade.gr/myDATA/invoice/v1.0">
<invoice>
<issuer>
<vatNumber>#####</vatNumber>
<country>GR</country>
</issuer>
<counterpart>
<vatNumber>####</vatNumber>
<country>GR</country>
<name>string</name>
</counterpart>
<invoiceHeader>
<branch>0</branch>
<series>1</series>
<aa>891</aa>
<issueDate>2017-11-26</issueDate>
<invoiceType>1.4</invoiceType>
<vatPaymentSuspension>true</vatPaymentSuspension>
<currency>EUR</currency>
</invoiceHeader>
<invoiceDetails>
<lineNumber>9870</lineNumber>
<measurementUnit>1</measurementUnit>
<invoiceDetailType>1</invoiceDetailType>
<netValue>5.00</netValue>
<vatCategory>1</vatCategory>
<vatExemptionCategory>2</vatExemptionCategory>
<discountOption>0</discountOption>
<withheldPercentCategory>10</withheldPercentCategory>
<feesAmount>0.00</feesAmount>
<feesPercentCategory>4</feesPercentCategory>
<otherTaxesPercentCategory>5</otherTaxesPercentCategory>
</invoiceDetails>
<invoiceSummary>
<totalNetValue>5.00</totalNetValue>
<totalVatAmount>0.00</totalVatAmount>
<totalWithheldAmount>0.00</totalWithheldAmount>
<totalFeesAmount>0.00</totalFeesAmount>
<totalStampDutyAmount>0.00</totalStampDutyAmount>
<totalOtherTaxesAmount>0.00</totalOtherTaxesAmount>
<totalDeductionsAmount>0.00</totalDeductionsAmount>
<totalGrossValue>5.00</totalGrossValue>
</invoiceSummary>
</invoice>
</InvoicesDoc>
我不知道从 XSD 到 XML 的转换或我在 c# 中的代码是否有问题。 我无法上传工具创建的最终 cs 文件,因为它太大了。
【问题讨论】:
-
XSD 在类上方创建了具有 [XmlRoot(ElementName="ABC")] 等属性的类。序列化方法默认为类名。您可以使用元素名称来覆盖类名称,这就是您获得 BookInvoiceType 而不是 InvoicesDoc 的原因。您还需要为发票添加另一个中间类以获得您想要的结果。
-
@jdweng你能给我一个带代码的小例子,我怎么能做到这一点。