1

Current Process

I have a valid XSD from Agresso website, which I have successfully converted into class, using xsd.exe

I can define all the objects I need, e.g.

    ABWInvoice oInvoiceAgresso = new ABWInvoice() { };
    List<ABWInvoiceInvoice> invoiceslist = new List<ABWInvoiceInvoice>();
    List<ABWInvoiceInvoiceDetail> invoicesDetailsList = new List<ABWInvoiceInvoiceDetail>();

and then to populate with the data

_parent.invoiceslist.Add(new ABWInvoiceInvoice
                {
                    InvoiceNo = _parent.InvoicesResultSet.Numeric1,

                    Header = new ABWInvoiceInvoiceHeader
                    {
                        InvoiceDate = _parent.InvoiceHeaders.InvoiceDate,
                        DueDate = _parent.vDueDate.Value,
                        OrderRef = _parent.InvoicesResultSet.GenericId,
                        Currency = _parent.Currency.Actual_Currency.TrimEnd(),
                        Seller = new ABWInvoiceInvoiceHeaderSeller { SellerNo = 1000 }
                    },
                    Details = _parent.invoicesDetailsList.ToArray(),
                    Summary = _parent.invoiceSummary
                });

I will spare the rest of the code... Eventually I get a valid XML file, which includes the following Date related elements, which come from agrlib namespace of the above XSD:

<InvoiceDate>2018-02-01</InvoiceDate>
<DueDate>2018-02-01</DueDate>

The Issue

Now a customer came back, saying they want to keep the reference to the agrlib, i.e. they want to have the following:

<agrlib:InvoiceDate>2018-02-01</InvoiceDate>
<agrlib:DueDate>2018-02-01</DueDate>

I've checked, using w3schools XML validator and it seems their request is absolutely valid, yet it might also mean they use their own parsing tool.

Question

How can I achieve their request in C#, please?

KDWolf
  • 398
  • 2
  • 14
  • You mean namespace should come with Element names? – FaizanHussainRabbani Feb 26 '18 at 09:44
  • Only for those, defined in agrlib. For example, currency and order reference should have no namespace reference: `2018-02-01 2018-02-01 806859 GBP` – KDWolf Feb 26 '18 at 09:50
  • 1
    Can you refer to following answer: https://stackoverflow.com/a/2339796/4222487 – FaizanHussainRabbani Feb 26 '18 at 09:54
  • You needed add to the class above the properties InvoiceDate and DueDate the following [XmlElement(ElementName = "InvoiceDate",Namespace ="the url of the namespace")] – jdweng Feb 26 '18 at 13:11
  • Thank you, jdweng. InvoiceDate is only an example. There are multiple elements. Does it man I shall manually add them all as per your example? – KDWolf Feb 26 '18 at 14:04
  • Am getting an exception "You need to add XmlChoiceIdentifierAttribute to the 'InvoiceDate' member" with `[XmlElement(ElementName = "InvoiceDate",Namespace ="the url of the namespace")]` Can you suggest anything, please? – KDWolf Feb 26 '18 at 22:57

1 Answers1

2

For anyone ending up here years later. Following the advice from the comment on the original question led me to get it working by doing as below.The necessary namespaces were defined in the XML file and Agresso have different types so select the ones that are necessary.

First add this property to the base class. It is needed for the xsi attribute

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://services.agresso.com/schema/ABWInvoice/2011/11/14")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://services.agresso.com/schema/ABWInvoice/2011/11/14", IsNullable=false)]
public partial class ABWInvoice
{
    /// <summary>
    /// This is required to make sure XML is exactly as original file
    /// </summary>
    [XmlAttributeAttribute("schemaLocation", AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string SchemaLocation = "http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd";
        
}

Then when serializing do the following:

XmlSerializer serializer = new XmlSerializer(typeof(ABWInvoice));
   
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("agr", "http://services.agresso.com/schema/ABWInvoice/2011/11/14");
namespaces.Add("agrlib",  "http://services.agresso.com/schema/ABWSchemaLib/2011/11/14");
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string file = "file.xml";
ABWInvoice someInvoice = new ABWInvoice();
using (var stream = new StreamWriter(file, false, Encoding.GetEncoding("ISO-8859-1")))
{
    serializer.Serialize(stream, someInvoice, namespaces);
}
John
  • 192
  • 3
  • 12