2014-03-26 81 views
2

由於輸入空InternalSubset:XDocument.Load()介紹了DTD頭

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

我的代碼返回:

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

空InternalSubset([]),其引入竊聽我,所以我試圖找出問題源於何處。事實證明,XDocument.Load()就是執行下面的罪魁禍首:

case XmlNodeType.DocumentType: 
    c.AddNodeSkipNotify(new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value, r.DtdInfo)); 

r.Value是一個空字符串,而不是null所以XDocument.DocumentType.InternalSubset是一個空字符串,而不是null

下面是示例代碼:

XDocument doc = new XDocument(
       new XDeclaration("1.0", Encoding.UTF8.WebName.ToUpper(), string.Empty), 
       new XDocumentType("cXML", null, "http://xml.cxml.org/schemas/cXML/1.2.020/cXML.dtd", null), 
       new XElement("cXML")); 

TextWriter writer = new StringWriter(); 
doc.Save(writer); 
doc.Dump(); 
doc = XDocument.Load(new StringReader(writer.ToString())); 
doc.Dump(); 
+0

**可能重複:** [從DTD頭卸下空方括號字符](http://stackoverflow.com/q/12358061/1497596) – DavidRR

回答

0

這應該解決當Load()之後調用該問題:

if (doc.DocumentType != null && doc.DocumentType.InternalSubset == string.Empty) 
{ 
    doc.DocumentType.InternalSubset = null; 
}