# save XML file via Interop class System.Xml.XmlDocument

**URL:** <https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475>\
**Category:** Developer Forum\
**Created:** [September 1, 2016, 1:56pm UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475 "2016-09-01T13:56:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshua.calandra](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@joshua.calandra](https://www.dynamicsuser.net/u/joshua.calandra)\
**Post date:** [September 1, 2016, 1:56pm UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/1 "2016-09-01T13:56:39Z")

</div>

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](http://dev.goshoom.net/en/2014/09/xml-doctype-in-x/) (a well done post from Martin) where it is explained how to workaround the createDocumentType() restriction from the XMLDocument class. Here my code

```auto
System.Xml.XmlDocument _xmlDoc;

_xmlDoc = new System.Xml.XmlDocument();

_xmlDoc.AppendChild(_xmlDoc.CreateDocumentType('MyType', null, 'http://www.validome.org/check/test.dtd', null));

_xmlDoc.Save("C:\TEMP\MyFile.xml");

```

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!

Joshua

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [September 1, 2016, 5:51pm UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/2 "2016-09-01T17:51:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joshua.calandra](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@joshua.calandra](https://www.dynamicsuser.net/u/joshua.calandra)\
**Post date:** [September 2, 2016, 9:38am UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/3 "2016-09-02T09:38:43Z")

</div>

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 "[www.validome.org/…/test.dtd"](http://www.validome.org/check/test.dtd) as shown in the tutorial. What am I missing now?  
Thanks.

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [September 2, 2016, 10:57am UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/4 "2016-09-02T10:57:12Z")

</div>

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 _[YourCompany.com/m2Data.dtd](http://YourCompany.com/m2Data.dtd)_ instead of just _m2Data.dtd_?

---

<div class="post-metadata">

**Author:** ![joshua.calandra](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@joshua.calandra](https://www.dynamicsuser.net/u/joshua.calandra)\
**Post date:** [September 2, 2016, 11:40am UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/5 "2016-09-02T11:40:08Z")

</div>

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 [YourCompany.com/m2Data.dtd](http://YourCompany.com/m2Data.dtd) instead of just m2Data.dtd?_

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

Thank you!

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [September 2, 2016, 12:25pm UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/6 "2016-09-02T12:25:28Z")

</div>

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.”_

---

<div class="post-metadata">

**Author:** ![joshua.calandra](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@joshua.calandra](https://www.dynamicsuser.net/u/joshua.calandra)\
**Post date:** [September 2, 2016, 3:30pm UTC](https://www.dynamicsuser.net/t/save-xml-file-via-interop-class-system-xml-xmldocument/61475/7 "2016-09-02T15:30:12Z")

</div>

Ok, now I got it, using

try  
{  
\_xmlDoc.AppendChild(\_xmlDoc.CreateDocumentType(‘SdDataSlice’, null, ‘m2Data.dtd’, null));  
}  
catch(Exception::CLRError)  
{  
info(AifUtil::getClrErrorMessage());  
}

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.
