【发布时间】:2014-12-09 16:46:04
【问题描述】:
我有一个从 Web 服务获得的 XML,我正在为它使用 HttpClient。这就是 XML 的样子:
<respuesta>
<entrada>
<rut>7059099</rut>
<dv>9</dv>
</entrada>
<status>
<code>OK</code>
<descrip>Persona tiene ficha, ok</descrip>
</status>
<ficha>
<folio>3204525</folio>
<ptje>7714</ptje>
<fec_aplic>20080714</fec_aplic>
<num_integ>2</num_integ>
<comuna>08205</comuna>
<parentesco>1</parentesco>
<fec_puntaje>20070101</fec_puntaje>
<personas>
<persona>
<run>7059099</run>
<dv>9</dv>
<nombres>JOSE SANTOS</nombres>
<ape1>ONATE</ape1>
<ape2>FERNANDEZ</ape2>
<fec_nac>19521101</fec_nac>
<sexo>M</sexo>
<parentesco>1</parentesco>
</persona>
<persona>
<run>8353907</run>
<dv>0</dv>
<nombres>JUANA DEL TRANSITO</nombres>
<ape1>MEDINA</ape1>
<ape2>ROA</ape2>
<fec_nac>19560815</fec_nac>
<sexo>F</sexo>
<parentesco>2</parentesco>
</persona>
</personas>
</ficha>
我正在尝试制作一个可以解析它的函数,现在(只是为了测试我对语言的理解,因为我是新手)我只需要它来找到“车辙”标签,第一个,或类似的东西。更准确地说,我需要在 XML 中找到一个值并返回它,这样我就可以在我的 .aspx 页面上的标签上显示它。我的解析函数代码如下:
public static String parseXml(String xmlStr, String tag)
{
String valor;
using (XmlReader r = XmlReader.Create(new StringReader(xmlStr)))
{
try
{
r.ReadToFollowing(tag);
r.MoveToContent();
valor = r.Value;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
return valor;
}
此代码基于我在 youtube 上找到的一个示例,该示例由微软的人制作,他们“解释”了如何使用解析器。
此外,这个函数是从 HttpClient 的一项任务内部调用的,就是这样:
protected void rutBTN_Click(object sender, EventArgs e)
{
if (rutTB.Text != "")
{
HttpClient client = new HttpClient();
String xmlString = "";
String text = "";
var byteArray = Encoding.ASCII.GetBytes("*******:*******"); //WebService's server authentication
client.BaseAddress = new Uri("http://wschsol.mideplan.cl");
var par = "mod_perl/xml/fps-by-rut?rut=" + rutTB.Text;
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.GetAsync(par).ContinueWith(
(requestTask) =>
{
HttpResponseMessage resp = requestTask.Result;
try
{
resp.EnsureSuccessStatusCode();
XmlDocument xmlResp = new XmlDocument();
requestTask.Result.Content.ReadAsStreamAsync().ContinueWith(
(streamTask) =>
{
xmlResp.Load(streamTask.Result);
text = xmlResp.InnerXml.ToString();
xmlString = parseXml(text, "rut"); //HERE I'm calling the parsing function, and i'm passing the whole innerXml to it, and the string "rut", so it searches for this tag.
Console.WriteLine("BP");
}).Wait();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}).Wait();
testLBL.Text = xmlString; //Finally THIS is the label i want to show the "rut" tag's value to be shown.
testLBL.Visible = true;
}
else
{
testLBL.Text = "You must enter an RUT number";
testLBL.Visible = true;
}
}
问题是,当我在解析函数中放置一些断点时,我可以看到它正在正确接收 innerxml 字符串(作为字符串),但它没有找到名为“rut”的标签,或者根本没有找到任何东西,因为它返回一个空字符串(“”)。
我知道这可能不是解析 xmlDocument 的正确方法,所以如果有人可以帮助我,我将非常感激。
编辑: 好的,所以我不会要求任何教程或类似的东西(我要求避免问菜鸟问题)。但无论如何,请不要只回答“你最好这样做”,如果你能解释一下“这就是你做错了什么,这就是你的代码不起作用”之类的事情,我将不胜感激,并且那么告诉我你们会怎么做。
提前致谢!
【问题讨论】:
-
“要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,描述问题以及迄今为止为解决问题所做的工作。”
-
你应该摆脱那个
try/catch/throw new Exception。异常会在没有您帮助的情况下传播,而您正在弄乱堆栈跟踪。