How do I add a document type to an XDocument?

后端 未结 2 1070
迷失自我
迷失自我 2021-02-07 13:18

I have an existing XDocument object that I would like to add an XML doctype to. For example:

XDocument doc = XDocument.Parse(\"test\");
         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 13:56

    Just pass it to the XDocument constructor (full example):

    XDocument doc = new XDocument(
        new XDocumentType("a", "-//TEST//", "test.dtd", ""),
        new XElement("a", "test")
    );
    

    or use XDocument.Add (the XDocumentType has to be added before the root element):

    XDocument doc = new XDocument();
    doc.Add(new XDocumentType("a", "-//TEST//", "test.dtd", ""));
    doc.Add(XElement.Parse("test"));
    

提交回复
热议问题