Reputation: 184
I am new in programming and especially in XML and XSD and I want help.
I have an XSD file which you can find here InvoicesDoc-v0.5.xsd
I convert it with Xsd2Code and with command
xsd InvoicesDoc-v0.5.xsd /classes
into classes and I try to create an XML string based on that XSD. My code for creating the XML is
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();
}
}
My problem is that When I create the XML string to send it it has the wrong format. The XML that I create has the following format
<?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>
and I want to have an output with the following format
<?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>
I don't know if I am doing something wrong with the convertion from XSD to XML or with my code in c#. I can't upload the final cs file that is created by the tools because it is too big.
Upvotes: 1
Views: 379
Reputation: 34421
You need to serialize InvoicesDoc and then change the classes to look like code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(InvoicesDoc));
InvoicesDoc doc = new InvoicesDoc()
{
invoice = new AadeBookInvoiceType[] {
new AadeBookInvoiceType()
{
}
}
};
serializer.Serialize(writer, doc);
}
}
public class InvoicesDoc
{
[XmlElement(ElementName = "invoice")]
public AadeBookInvoiceType[] invoice { get; set; }
}
[XmlRoot(ElementName = "invoice")]
public class AadeBookInvoiceType
{
//add the rest of the invoice properties here
}
}
Upvotes: 3