I am having a problem while parsing an url of an intranet site with xml document. The following is a simplified example :
XML file
<?xml version="1.0" encoding="utf-8" ?>
<Nodes>
<Node>
<Project>Test</Project>
<Link>https://www.google.com/?gws_rd=ssl#q=&+fails+in+url</Link>
</Node>
</Nodes>
When I try to parse and load the xml above in my c# code, I get an error at "Xdoc.load" because of the "&" used in the above code. Generally, we can resolve this by using "%26" in place of &, but I can't in my case as changing the "&" to a "%26" is breaking the url. ie. I think the "&" is being used as part of a query string and removing the & is breaking the parameters on the page.
This might not be the efficient way to do it, but this is the requirement.
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("~/Content/XMLFile1.xml"));
XmlNodeList lNodes = xdoc.SelectNodes("/Nodes/Node");
foreach (XmlElement p in lNodes)
{
var m = p["Link"].InnerText;
string s = "window.open('" + m + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
//ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", s, true);
}
}
<Link><![CDATA[url]]></Link>. Otherwise you'll have to manipulate the XML prior to loading it intoXmlDocument, and that is a very brittle approach and not a good idea.&instead of&in xml.