使用XPath查询XML文档对象

时间:2011-09-19 10:24:36

标签: c# xml visual-studio-2010 xslt sharepoint-2010

我目前有一个带有提交按钮的webpart和两个用于电话号码和邮政编码的文本字段。当电话号码和邮政编码输入文本文件并按下提交时,我希望能够提交查询和外部API的查询字符串,然后在XML文档对象的表单中返回查询结果。然后,我需要使用XPath查询此对象以显示结果然后格式化或将此结果转换为HTML。 首先:

  1. 我创建了XML文档对象(在类中),但我不太清楚如何使用XPath查询此对象以检索结果。

  2. 我也不太清楚如何连接提交按钮以执行上述XMLDocument对象中的查询字符串。

  3. 下面是XMLDocument对象,它将查询外部API并返回结果,在代码中没有任何错误,所有这些都是好的:

    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'};");       
    
    }
    
    
    
    
    void cmdSubmit_Click(object sender, EventArgs e)
    {
    
    
    
    
    
    
    
    }
    
    }
    }
    

    在Sharepoint 2010中没有使用XML做很多事情,所以非常感谢上面第1点和第2点的任何帮助和帮助!

    提前致谢

1 个答案:

答案 0 :(得分:0)

要使用XPath选择XmlDocument中的节点,您可以使用方法SelectSingleNodeSelectNodes