BOR15K
BOR15K

Reputation: 468

How to keep a colon, when creating XML from an XSD based class in C#?

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?

Upvotes: 1

Views: 234

Answers (1)

John
John

Reputation: 359

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);
}

Upvotes: 2

Related Questions