引用了未声明的实体“NBSP”,而从URL在C#中读取XML?实体、声明、NBSP、URL

2023-09-02 02:03:59 作者:孩子他娘,快到怀里来!

 的XmlDocument xmlDoc中=新的XmlDocument();
    xmldoc.XmlResolver = NULL;

    xmldoc.Load(URL);
    的XmlWriter xmlWrite = XmlWriter.Create(@使用Server.Mappath(的test.xml));
    xmldoc.Save(xmlWrite);
    xmlWrite.Close();
 

以上是code我使用读取XML文件。我加载XML包含类似实体 NBSP; 正因为如此,在code被抛出XMLException:

  

引用了未声明的实体NBSP的

解决方案

有两种方式来处理这个问题,但为你需要的修改输入文件。

1)改变你的&放大器; NBSP; &放大器;#160; 在你输入文件。它始终是更好地使用统一code和避免在XML文档中读取的实体。使用普通的Uni code字符或数字的形式。

2)如果你还需要/想使用&放大器; NBSP; 你可以声明自定义文档类型的文件中,将做转换为你:

 <!DOCTYPE doctypeName [
   <!实体NBSP&放大器;#160;>
]≥
 
XML实体注入漏洞的利用与学习

这个问题来,因为&放大器; NBSP; 不是XMLS predefined实体之一

    XmlDocument xmldoc = new XmlDocument();
    xmldoc.XmlResolver = null;

    xmldoc.Load("URL");
    XmlWriter xmlWrite = XmlWriter.Create(@Server.MapPath("Test.xml"));
    xmldoc.Save(xmlWrite);
    xmlWrite.Close();

Above is the code I am using to read an XML file. The XML I am loading contains entities like nbsp; and because of that, the code is throwing an XMLException:

Reference to undeclared entity 'nbsp'

解决方案

There are two ways to handle this, but for both you need to make changes to the input file.

1) Change your   to   in you input files. It is always better to use Unicode and avoid readable entities in XML documents. Use plain Unicode characters or their numerical form.

2) If you still need/want to use   You can declare a custom doctype in the file that will do the conversion for you:

<!DOCTYPE doctypeName [
   <!ENTITY nbsp "&#160;">
]> 

This problem is coming because &nbsp; isn't one of XMLs predefined entities.