How to perform DTD verification for an XML file

Source: Internet
Author: User

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 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.