Reputation: 773
I have completed the class structure necessary for SEPA xml file serialization and deserialization.
I can write the xml file to my local drive as required.
What I can't do is change the document header file to look like the 'REQUIRED' version below when the xml file is written to my local drive:
REQUIRED :
< Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02">
ACTUAL : < Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02">
So you can see what i need and what the actual output i get. I will now show you the code in my Document class and how I am attempting to change the header text.
This is the main root of the xml file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using SEPABuilder.FileSection;
namespace SEPAFileSection
{
[XmlRoot("Document")]
public class Document
{
#region Fields
private CstmrDrctDbtInitn _cstmrDrctDbtInitn;
#endregion
#region Constructors
public Document()
{
}
#endregion
#region Properties
/// <summary>
/// Only 1 CstmrDrctDbtInitn per document
/// </summary>
[XmlElement("CstmrDrctDbtInitn")]
public CstmrDrctDbtInitn CstmrDrctDbtInitn
{
get
{
if (_cstmrDrctDbtInitn == null)
_cstmrDrctDbtInitn = new CstmrDrctDbtInitn();
return _cstmrDrctDbtInitn;
}
set
{
_cstmrDrctDbtInitn = value;
}
}
#endregion
#region Methods
#endregion
}
}
Some of the examples I have referenced from this site in an attempt to solve this issue are :
How can I skip xml declaration when serializing? Including xmlns in my XML file
... and this is a code extract of how I am attempting to change the header text after viewing these examples:
SEPAFileSection.Document doc = new SEPAFileSection.Document();
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
XNamespace ns1 = "http://www.w3.org/2001/XMLSchema-instance";
namespaces.Add("xsi", ns1.ToString());
XNamespace ns2 = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02";
namespaces.Add("xmlns",ns2.ToString());
//namespaces.Add(string.Empty,ns.ToString());
XmlSerializer serializer = new XmlSerializer(typeof(SEPAFileSection.Document));
using (TextWriter writer = new StreamWriter(@"C:\Xml.txt"))
{
serializer.Serialize(writer, doc, namespaces);
}
So, the above code writes out my XML file fine but as you can see from the 'ACTUAL' document header above, i have an extra xmlns... i tried to use "" blank but it wouldnt allow it.
I have attached the 'Document' class here only as it is the main parent, I haven't included all the child classes as they are working fine. I hope this clarifies everything?
Many thanks in advance for any assistance.
Upvotes: 1
Views: 1867
Reputation: 33381
Try this in addition with XmlSerializerNamespaces
:
[XmlRoot(ElementName = "Document", Namespace = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02")]
public class Document
{
...
}
and remove
XNamespace ns2 = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02";
namespaces.Add("xmlns",ns2.ToString());
Notice that ElementName = "Document"
is redundant.
Upvotes: 1