Querying an XML Document Object using XPath
I currently have a webpart with a submit button and two text fields for phone number and postcode. When a phone number and a postcode are entered into the text fileds and the submit is pressed, I want to be able to submit a query string that queries and external API and then returns the query results in the Form of an XML document Object. I then need to query this object using XPath to display the results then format or convert this results to HTML. Firstly:
I have created the XML Document Object (in a class) but I am not too sure how to then query this object using XPath to retrieve the results.
I am also not too sure how to wire up the submit button to execute the query string in the XMLDocument object mentioned above.
Below is the XMLDocument Object that will query an external API and return results there are no errors in the code sugesting all is well so far:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml; // needed for the XML document object
using System.Xml.XPath; //needed to use Xpath to query the XMLDocument object
using System.Xml.Xsl; //needed to convert xml into HTML hopefully
namespace ACWebPart.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
private XmlDocument performXmlCheck(string user, string pass, string phone, string postcode,
string checks, string options)
{
//creating the XMLDocument object
XmlDocument xml = new XmlDocument();
//creating the query to run against the server
string url = String.Format("http://api.samknows.com/checker.do?user= {0} &pass={1} &phone={2}
&postcode={3} &checks={4} &options={5} &output=xml", user, pass, phone, postcode, checks,
options);
//to catch errors a try and catch will be created
try
{
//querying the server, parse the XML and store it
xml.Load(url);
}
catch
{
//if an execption is encountered (Server unreachable, HTTP500, HTTP 404, etc) return null
return null;
}
return xml;
}
protected void Page_Load(object sender, EventArgs e)
{
//creating the event habdler for the submit button
cmdSubmit.Click += new EventHandler(cmdSubmit_Click);
//Live validation of input text boxes to ensure they are not empty and have the correct input
format
TextBox1.Attributes.Add("OnFocus", "if(this.value == 'Phone number') {this.value='',
this.style.borderColor='#c4c4c4', this.style.color='#676767', this.style.fontStyle='normal'};");
TextBox1.Attributes.Add("OnBlur", "if(this.value == '') {this.value='Phone number',
this.style.color='red', this.style.fontStyle='italic', this.style.borderColor='red'};");
TextBox2.Attributes.Add("OnFocus", "if(this.value == 'Postcode's) {this.value='',
this.style.borderColor='#c4c4c4', this.style.color='#676767', this.style.fontStyle='normal'};");
TextBox2.Attributes.Add("OnBlur", "if(this.value == '') {this.value='Postcode',
this.style.color='red', this.style.fontStyle='italic', this.style.borderColor='red'};");
}开发者_Python百科
void cmdSubmit_Click(object sender, EventArgs e)
{
}
}
}
Havent done much with XML in Sharepoint 2010 so any help and assistance with points 1 and 2 above would be much appreciated!
Thanks in advance
To select nodes in your XmlDocument
using XPath you can use the methods SelectSingleNode and SelectNodes.
精彩评论