If you need to verify the DTD of an XML file, you usually need to embed a DTD declaration in the XML file to reference the path of the DTD file. However, for XML documents uploaded to Web server, users cannot specify DTD declarations in their XML documents. Therefore, we need to add a DTD declaration to the uploaded XML document. Record it hereCode. My DTD file is embeddedProgramTherefore, you need to extract the DTD file from the program set and put it on the disk, and then add the DTD declaration.
1 Public Void Verifyxmlfile (xmldocument DOC)
2 {
3 // Extract DTD file from embedded source
4 Assembly currentassembly = Assembly. getexecutingassembly ();
5 Stream dtdstream = Currentassembly. getmanifestresourcestream ( " Myprovider. " + Dtd_file_name );
6 String Dtdpath = Path. Combine (path. getdirectoryname (currentassembly. Location), dtd_file_name );
7
8 If (File. exists (dtdpath) = False )
9 {
10 Streamreader SR = New Streamreader (dtdstream, encoding. Default );
11 File. writealltext (dtdpath, Sr. readtoend ());
12 Sr. Close ();
13 Dtdstream. Close ();
14 }
15
16 Doc. insertbefore (Doc. createdocumenttype ( " Currencyexchange " , Null , Dtdpath, Null ), Doc. documentelement );
17 Memorystream xmlstreamwithdtd = New Memorystream ();
18 Doc. Save (xmlstreamwithdtd );
19 Xmlstreamwithdtd. Position = 0 ;
20
21 Xmlreadersettings settings = New Xmlreadersettings ();
22 Settings. prohibitdtd = False ;
23 Settings. validationtype = Validationtype. DTD;
24 Xmlreader = Xmlreader. Create (xmlstreamwithdtd, settings );
25 Try
26 {
27 While (Reader. Read ())
28 {
29 }
30 }
31 Catch (Exception ERR)
32 {
33 Throw Err;
34 }
35 Finally
36 {
37 Reader. Close ();
38 Xmlstreamwithdtd. Close ();
39 }
40 }