How do I generate a linq to xml infoset w/ a DTD reference?
I need to generate an xml infoset but the infoset needs to contain a reference to a client's DTD. The desired out needs to contain this DTD reference
<!DOCTYPE AutoApplication SYSTEM "http://www.clientsite.com/pu开发者_Go百科blic/DTD/autoappV1-3-level2.dtd">
This reference sits directly benath the xml declaration. Neither XProcessingInstruction or XDeclaration do the job, is there another type I need to use?
you need to add your dtd using a XDocumentType object. see here for more info. It should be noted that xlinq has pretty limited processing for DTD's, though (see msdn).
some sample code....
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass
{
public static void Main()
{
XDocument xDocument = new XDocument();
XDocumentType documentType = new XDocumentType("Books", null, "Books.dtd", null);
xDocument.Add(documentType, new XElement("Books"));
Console.WriteLine(xDocument);
}
}
For this xml fragment.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE AutoApplication SYSTEM "http://www.clientsite.com/public/DTD/autoappV1-3-level2.dtd">
We would do:
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XDocumentType("AutoApplication", null, "http://www.clientsite.com/public/DTD/autoappV1-3-level2.dtd", null));
);
精彩评论