问题
I want to be able to load aspx page into XmlDocument variable. How do I do that? Here is what I have tried and its expecting .xml file and not .aspx page. Is there any way to convert aspx page on the fly into xml document and load it? 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. Line 1, position 2.
回答1:
The reason you're getting that error:
Name cannot begin with the '%' character, hexadecimal value 0x25. Line 1, position 2.
is because .aspx
pages are often not valid XML. ASP.NET .aspx
pages contain directives such as:
<%@ Page Language="C#" [possibly other stuff] %>
<%@
and %>
is not valid XML which is why you can't load the raw ASPX page.
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.
You might want to try and load the page (with or without directives) using the HTML Agility Pack which can be downloaded via NuGet.
回答2:
XmlDocument represents Xml so if you try to load an aspx it will throw errors complaining not valid xml
But if the response coming from .aspx is e.g "<?xml..... ><Employee>dotNetMan</Employee>"
as string then yes it will work
You will have to set the content-type to 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
来源:https://stackoverflow.com/questions/11443441/load-aspx-file-into-xmldocument