简体   繁体   中英

Replace cXML DOCTYPE declaration

I am looking to replace the DOCTYPE declaration from incoming cXML documents:

<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.029/cXML.dtd">

and end up with <!DOCTYPE cXML>

I currently use a matching if cXML.Contains(matching value).Replace("DOCTYPE cXML"); the problem is that the value is variable. Is there a wildcard option ?

string xml = @"<!DOCTYPE cXML SYSTEM ""http://xml.cxml.org/schemas/cXML/1.2.029/cXML.dtd"">
                 <cXml>
                 </cXml>
                ";

XDocument doc = XDocument.Parse(xml);
var name = doc.DocumentType.Name;

var docType = new XDocumentType(name, null, null, null);

doc.DocumentType.ReplaceWith(docType);

Console.WriteLine(doc.ToString());

You need to create new XDocumentType and after that replace the previous one.

You can you below code sample to removing and adding DOCTYPE from cXML

XmlDocument XDoc = new XmlDocument();
XDoc.Load(sXMLFile);
XmlDocumentType XDType = XDoc.DocumentType;
XDoc.RemoveChild(XDType); 

something same for adding your DocType

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM