2014-03-26 56 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; 
}