我有理由不能从以下两个元素中获取值吗?

时间:2011-10-27 15:30:08

标签: c# xml xslt sharepoint-2010

我有代码提交查询,结果以xml格式然后被分配给字符串变量“tmp”然后我将此变量作为xml输入提供给一个函数,该函数使用单独的xslt样式表将xml结果转换为HTML。

XML:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<Results>
<Exchange>
<OLO>LSMIT</OLO>
<Name>Mitcham</Name>
</Exchange>
<Checks>
<Check id="adsl">
<Linecode>GGEZ</Linecode>
<Linespeed>2048</Linespeed>
<Matched>Address</Matched>
<Provider>BT ADSL</Provider>
<Type>BT xDSL</Type>
<Updated>2010-08-17</Updated>
</Check>
</Checks>
</Results>

XSLT样式表:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>

<xsl:template match="/">
<html>
<body>
<p>You are connected to the <b>
<xsl:for-each select="Results/Exchange">
<xsl:value-of select="Name"/>
</xsl:for-each>
</b> telephone exchange</p>
<br/>

<p>According to <b>
<xsl:for-each select="Results/Checks/Check">
<xsl:value-of select="Provider"/>
</xsl:for-each>
</b>, houses at your postcode should be able to suppor the following:</p>
<br/>

<table>
<tr>
<td>
<img src="/img/tick_BGAvailabilityChecker.png"></img>  
</td>
<td>
up to 
<xsl:for-each select="Results/Checks/Check">
<xsl:value-of select="Linespeed"/>
</xsl:for-each> 
</td>
</tr>
</body>
</html>
</xsl:template>

我无法显示以下元素的evalues:

<Linecode>
<Linespeed>
<Matched>

然而,我可以很好地显示以下元素:

<Provider>
<Type>
<Updated>

以上所有都是元素的孩子

这是一个奇怪的问题,问题是为什么我不能显示Linecode,Linespeed,Matched elemets的值?

我花了太多时间在这上面而没有能够确定某些元素正在显示的内容而其他元素没有。任何想法将不胜感激,

这是我写的用于提交查询并将xml结果存储到字符串变量“tmp”的代码:

void cmdSubmit_Click(object sender, EventArgs e)
{
//Variable declarations
string user = "PRIVATE";
string pass = "PRIVATE";
string phone = TextBox1.Text;
string postcode = TextBox2.Text;
string buildnum = TextBox3.Text;
string check = "adsl";
string option = "adsllinecheck";
string outputformat = "xml";

string url = String.Format("http://api.samknows.com/checker.do?user={0}&pass={1}&phone=
{2}&postcode={3}&buildingnum={4}&checks={5}&options{6}&output{7}", user, pass, phone, 
postcode, buildnum, check, option, outputformat);
Uri uri = new Uri(url);

 string data = "field-keywords=ASP.NET 3.5";

 if (uri.Scheme == Uri.UriSchemeHttp)
 {
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
 request.Method = WebRequestMethods.Http.Post;
 request.ContentLength = data.Length;
 request.ContentType = "application/x-www-form-urlencoded";

 StreamWriter writer = new StreamWriter(request.GetRequestStream());
 writer.Write(data);
 writer.Close();
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 StreamReader reader = new StreamReader(response.GetResponseStream());
 string tmp = reader.ReadToEnd();

在调试之后,它会发现没有返回元素。但是,当我只是将查询字符串复制到浏览器中时,xml中的结果会显示相关元素。

在提交查询以确保返回所有元素的方式上,我能做些什么不同吗?

由于

2 个答案:

答案 0 :(得分:0)

理论上回答你的问题。是的,有很多原因,你的xslt转换不是其中之一。应用于xml的xslt的输出是:

<html>
   <body>
      <p>
         You are connected to the <b>Mitcham</b> telephone exchange

      </p><br><p>
         According to <b>BT ADSL</b>, houses at your postcode should be able to suppor the following:

      </p><br><table>
         <tr>
            <td><img src="/img/tick_BGAvailabilityChecker.png"></td>
            <td>
               up to
               2048
            </td>
         </tr>
      </table>
   </body>
</html>

与您声称无法显示Linespeed元素形成鲜明对比。错误发生在您未显示的代码的其他部分。

答案 1 :(得分:0)

事实证明,代码没有错误。在查看查询字符串之后:

string url = String.Format("http://api.samknows.com/checker.do?user={0}&pass={1}&phone=
{2}&postcode={3}&buildingnum={4}&checks={5}&options{6}&output{7}"

我注意到选项和输出参数中缺少assignement运算符(=)。添加后,一切都完美无缺....

愚蠢的错误,最终解决了问题!

感谢他所有的建议。