简体   繁体   English

将aspx文件加载到xmldocument中

[英]load aspx file into xmldocument

I want to be able to load aspx page into XmlDocument variable. 我希望能够将aspx页面加载到XmlDocument变量中。 How do I do that? 我怎么做? Here is what I have tried and its expecting .xml file and not .aspx page. 这是我尝试过的和它期待的.xml文件而不是.aspx页面。 Is there any way to convert aspx page on the fly into xml document and load it? 有没有办法将aspx页面即时转换为xml文档并加载它? thanks 谢谢

string filePath = @"C:\WebApplication1\webform4.aspx";
XmlDocument document = new XmlDocument();
document.Load(filePath);

I get the following error: 我收到以下错误:

Name cannot begin with the '%' character, hexadecimal value 0x25. 名称不能以'%'字符开头,十六进制值0x25。 Line 1, position 2. 第1行,第2位。

The reason you're getting that error: 你收到这个错误的原因:

Name cannot begin with the '%' character, hexadecimal value 0x25. 名称不能以'%'字符开头,十六进制值0x25。 Line 1, position 2. 第1行,第2位。

is because .aspx pages are often not valid XML. 是因为.aspx页面通常不是有效的XML。 ASP.NET .aspx pages contain directives such as: ASP.NET .aspx页面包含如下指令:

<%@ Page Language="C#" [possibly other stuff] %>

<%@ and %> is not valid XML which is why you can't load the raw ASPX page. <%@%>是无效的XML,这就是您无法加载原始ASPX页面的原因。

Now, even if you were to strip out these directives, there's a fairly good chance that unless you've been really, really strict and all of the markup on the page is XHTML, then that won't load either. 现在,即使你要删除这些指令,除非你真的非常非常严格并且页面上的所有标记都是XHTML,否则它很可能也不会加载。

You might want to try and load the page (with or without directives) using the HTML Agility Pack which can be downloaded via NuGet. 您可能想尝试使用可通过NuGet下载的HTML Agility Pack加载页面(带或不带指令)。

XmlDocument represents Xml so if you try to load an aspx it will throw errors complaining not valid xml XmlDocument表示Xml,因此如果您尝试加载aspx,它将抛出错误,抱怨无效的xml

But if the response coming from .aspx is eg "<?xml..... ><Employee>dotNetMan</Employee>" as string then yes it will work 但是如果来自.aspx的响应是例如"<?xml..... ><Employee>dotNetMan</Employee>"作为字符串那么是的它会起作用

You will have to set the content-type to text/xml 您必须将content-type设置为text/xml

Here is a good example how to return xml from aspx http://www.benhblog.com/2008/07/returning-xml-from-aspx-page.html 这是一个很好的例子,如何从aspx返回xml http://www.benhblog.com/2008/07/returning-xml-from-aspx-page.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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