Question
Can I modify my POCO classes so that when serialised they contain the ENTITYs required? After reading this from W3C and this answer to a similar question I realised my XML should contain the DOCTYPE as below, I just don't know how to insert it.
<?xml version="1.0" encoding="utf-16"?>
<!DOCTYPE documentElement[<!ENTITY NZ "NZ"><!ENTITY AU "AU">]>
<ValuationTransaction xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://vx.valex.com.au/lixi/schema/1.3.5/ValuationTransaction.xsd" ProductionData="Yes">
... etc.
</ValuationTransaction>
My serialising code looks like this
public static string ToXmlString(this ValuationTransaction payloadPoco)
{
var stringwriter = new StringWriter();
var serializer = new XmlSerializer(payloadPoco.GetType());
serializer.Serialize(stringwriter, payloadPoco);
return stringwriter.ToString();
}
Background
I'm serialising my root class to a string using an XmlSerializer and a StringWriter, then validating the XML string using an XmlValidator from this post. While validating the XML the validator says...
Reference to an undeclared entity, 'NZ'. at line 37 position 24
... which refers to the ISO3166 attribute of this element
<Country ISO3166="NZ">NEW ZEALAND</Country>
... the definition for which is:
[System.Xml.Serialization.XmlAttributeAttribute(DataType = "ENTITY")]
public string ISO3166
{
get { return this.iSO3166Field; }
set { this.iSO3166Field = value; }
}
The range of values is limited for my purposes to NZ and AU. I can add the DOCTYPE like this, though obviously I'd rather find a better way:
var entitiesDtd = @"<!DOCTYPE documentElement[<!ENTITY NZ ""NZ""><!ENTITY AU ""AU"">]>" + Environment.NewLine;
xmlString = xmlString.Insert(xmlString.IndexOf("<ValuationTransaction"), entitiesDtd);
The definition for the XML root class (generated by xsd.exe and manually tweaked to add noNamespaceSchemaLocation) is
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ValuationTransaction
{
[XmlAttribute("noNamespaceSchemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string noNamespaceSchemaLocation = "https://vx.valex.com.au/lixi/schema/1.3.5/ValuationTransaction.xsd";
... etc.
}