积极答复者
XmlDocument.LoadXml加载html页面效率低的有关疑问

问题
-
中文版
最近做了一个向标准的html页面中读取相关信息的程式,方法已经实现,但是效率不高。仔细研究发现时XmlDocument.LoadXml方法在加载页面代码过程中耗费了过多时间。为了能简单表述问题,我将问题简化成了如下代码:System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); string xmlstr = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\r\n<title>新闻阅读</title>\r\n<link type=\"text/css\" rel=\"stylesheet\" href=\"../../NewsReaderStyle.css\" />\r\n</head>\r\n\r\n<body>\r\n<div id=\"title\">猪流感来临 “宅男”成老公上上选?</div>\r\n<div id=\"status\">2009年05月03日 <span id=\"source\">武汉晚报</span></div>\r\n<div id=\"content\">猪流感来临 “宅男”成老公上上选?</div>\r\n</body>\r\n</html>\r\n"; xmlDoc.LoadXml(xmlstr.Replace("\r\n", string.Empty)); Response.Write(xmlDoc.GetElementById("content").InnerXml);
结果在LoadXml方法那里停留了大概6秒的时间。对于一个文件操作超过1秒就已经很是问题了,6秒就意味着该方案无法通过。而且不管我如何简化html字符串,需要加载的时间仍旧是6秒左右。对于msdn上的例子doc.LoadXml("<item><name>wrench</name></item>");运行的时间的确很短,但是我想问,为何前者耗时6秒呢?LoadXml本身的机制如何?
English Version
Recently,I do a programe of fetching informations from standard html page,and I have done that. Unfortunately,the code runs inefficiently。I fount the touble is that it's cost too much time on the method XmlDocument.LoadXml while loading the html code。I show a simple code below in order to make the problem clearly.System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); string xmlstr = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\r\n<title>新闻阅读</title>\r\n<link type=\"text/css\" rel=\"stylesheet\" href=\"../../NewsReaderStyle.css\" />\r\n</head>\r\n\r\n<body>\r\n<div id=\"title\">猪流感来临 “宅男”成老公上上选?</div>\r\n<div id=\"status\">2009年05月03日 <span id=\"source\">武汉晚报</span></div>\r\n<div id=\"content\">猪流感来临 “宅男”成老公上上选?</div>\r\n</body>\r\n</html>\r\n"; xmlDoc.LoadXml(xmlstr.Replace("\r\n", string.Empty)); Response.Write(xmlDoc.GetElementById("content").InnerXml);
In surprise, the code spend 6 seconds in the method LoadXml and it's too long to wait for a file operation. In another way, I did try the example of msdn [doc.LoadXml("<item><name>wrench</name></item>");], it runs rapidly. So what's going on? Who can tell me how to do!- 已移动 Sheng Jiang 蒋晟 2009年5月7日 5:56 XML问题 ([Loc]From:Visual C#)
2009年5月7日 4:14
答案
-
参考一下 http://www.bobopo.com/article/code/xmldocument_xhtml.htm
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2009年5月14日 6:15
2009年5月7日 5:03 -
xml dom 的建立是很费时间的 它要根据你的标记不断的生成对象 而html文件的小标记格外的多
建议使用 xmlreader
工作突然有点忙 嘿嘿- 已标记为答案 KeFang Chen 2009年5月14日 6:14
2009年5月8日 3:17
全部回复
-
中文版
如果只是从XML对象读取数据,用只读的XPathDocument代替XMLDocument,可以提高性能。
English Version
If you are only reading from the XML object, avoid using XMLDocumentt, instead use XPathDocument, which is readonly and so improves performance.
//避免
XmlDocument xmld = new XmlDocument();
xmld.LoadXml(sXML);
txtName.Text = xmld.SelectSingleNode("/packet/child").InnerText;
//推荐
XPathDocument xmldContext = new XPathDocument(new StringReader(oContext.Value));
XPathNavigator xnav = xmldContext.CreateNavigator();
XPathNodeIterator xpNodeIter = xnav.Select("packet/child");
iCount = xpNodeIter.Count;
xpNodeIter = xnav.SelectDescendants(XPathNodeType.Element, false);
while(xpNodeIter.MoveNext())
{
sCurrValues += xpNodeIter.Current.Value+"~";
}- 已编辑 邹俊才 2009年5月7日 5:01
2009年5月7日 5:00 -
参考一下 http://www.bobopo.com/article/code/xmldocument_xhtml.htm
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2009年5月14日 6:15
2009年5月7日 5:03 -
xml dom 的建立是很费时间的 它要根据你的标记不断的生成对象 而html文件的小标记格外的多
建议使用 xmlreader
工作突然有点忙 嘿嘿- 已标记为答案 KeFang Chen 2009年5月14日 6:14
2009年5月8日 3:17