save XML file via Interop class System.Xml.XmlDocument

Hi all,

have a problem saving my just created XML doc into a file using the method Save() under the interop class System.Xml.XmlDocument. My necessity to use this .NET class comes from here (a well done post from Martin) where it is explained how to workaround the createDocumentType() restriction from the XMLDocument class. Here my code

System.Xml.XmlDocument _xmlDoc;

_xmlDoc = new System.Xml.XmlDocument();

_xmlDoc.AppendChild(_xmlDoc.CreateDocumentType('MyType', null, '', null));


When I execute this on a job, it dies in the saving line part of the code. Do not understand actually why? Can someone help me out?

Thank you in advance!


First of all, provide a valid path. You don’t get what you intended because you didn’t escape backslashes. Use either “C:\TEMP\MyFile.xml” or @“C:\TEMP\MyFile.xml”.

There may be other problems, but this must be fixed in either case.

Thank you Martin, that works out. One more question, I now tried to customize the DTD string to have something like this as result

and here the code I use

_xmlDoc.AppendChild(_xmlDoc.CreateDocumentType(‘SdDataSlice’, null, ‘m2Data.dtd’, null));

but it dies and works only when using your link "…/test.dtd" as shown in the tutorial. What am I missing now?

Unfortunately I don’t have any AX 2012 on hand, so I can’t try it in the moment. Can you elaborate what you mean by “it dies”? What if you use an URI such as instead of just m2Data.dtd?

Hi Martin,

thanks for the reply, when I say “dies” I mean that the elaboration stops/ aborts as so if I put the code into a try/catch I immediately go into the catch statement.

What if you use an URI such as instead of just m2Data.dtd?

So it must be an URI?because I just have the filename.

Thank you!

What’s the type and message of the exception?

Yes, it must be an URI, as far as I know. The documentation for CreateDocumentType() says: “The system identifier of the document type or null. Specifies the URL of the file location for the external DTD subset.”

Ok, now I got it, using

_xmlDoc.AppendChild(_xmlDoc.CreateDocumentType(‘SdDataSlice’, null, ‘m2Data.dtd’, null));

I get the following

Could not find file ‘C:\Windows\system32\m2Data.dtd’.

So I replaced code like

_xmlDoc.AppendChild(_xmlDoc.CreateDocumentType(“SdDataSlice”, null, @“C:\TEMP\test.dtd”, null));

and I get my XML

not exactly what I need but now I now where to act in my code. Thank as always Martin to point me to the correct solution!

Have a great day.