循环返回 XML 结果并赋值
Loop through returned XML Result and assign values
我目前正在调用一个外部 MVC 控制器并传入两个参数,这两个参数传回 XML 结果我目前的操作如下,请注意我在 [=11= 方面的经验] 几乎是 0
try
{
var url = ConfigurationManager.AppSettings["url"] + String.Format("/ws/RetailerBrandsFiltered?postcode={0}&countryCode={1}", model.Postcode, model.SelectedCountry);
// Create the request to call the external mvc controller to retrieve the retailers
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "xml";
// Do the request to external controller
var stIn = new StreamReader(req.GetResponse().GetResponseStream());
var responce = stIn.ReadToEnd();
foreach (var item in responce)
{
string urn = item["URN"].InnerText
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
我正在尝试遍历响应并取出 XML 节点并将它们分配给一个模型(我还没有实现)但是为了测试我正在尝试获取第一个值并将其分配给字符串。
这不起作用,消息是无法将索引应用于类型 char,理想情况下我的 for 循环看起来像这样。
foreach (var item in responce)
{
string urn = item["URN"].InnerText;
string name = item["NAME"].InnerText;
string address = item["ADDRESS"].InnerText;
}
同样,一旦它起作用,我将用模型绑定替换字符串。任何帮助将不胜感激
变量 reponce
是一个字符串,当您尝试对其进行迭代时,您隐式地将字符串转换为 char[]
并对其进行迭代。
将流加载到 XDocument
并使用 Linq 获取值:
try
{
var url = ConfigurationManager.AppSettings["url"] + string.Format("/ws/RetailerBrandsFiltered?postcode={0}&countryCode={1}", model.Postcode, model.SelectedCountry);
// Create the request to call the external mvc controller to retrieve the retailers
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "xml";
// Do the request to external controller
var xDoc = XDocument.Load(req.GetResponse().GetResponseStream());
// Get some nodes
var items = xDoc.Descendants("item");
foreach(var item in items)
{
string urn = item.Element("URN").Single().Value;
// Do more stuff
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
请参阅 MSDN article and this Stack Overflow question 了解更多详情。
我目前正在调用一个外部 MVC 控制器并传入两个参数,这两个参数传回 XML 结果我目前的操作如下,请注意我在 [=11= 方面的经验] 几乎是 0
try
{
var url = ConfigurationManager.AppSettings["url"] + String.Format("/ws/RetailerBrandsFiltered?postcode={0}&countryCode={1}", model.Postcode, model.SelectedCountry);
// Create the request to call the external mvc controller to retrieve the retailers
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "xml";
// Do the request to external controller
var stIn = new StreamReader(req.GetResponse().GetResponseStream());
var responce = stIn.ReadToEnd();
foreach (var item in responce)
{
string urn = item["URN"].InnerText
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
我正在尝试遍历响应并取出 XML 节点并将它们分配给一个模型(我还没有实现)但是为了测试我正在尝试获取第一个值并将其分配给字符串。
这不起作用,消息是无法将索引应用于类型 char,理想情况下我的 for 循环看起来像这样。
foreach (var item in responce)
{
string urn = item["URN"].InnerText;
string name = item["NAME"].InnerText;
string address = item["ADDRESS"].InnerText;
}
同样,一旦它起作用,我将用模型绑定替换字符串。任何帮助将不胜感激
变量 reponce
是一个字符串,当您尝试对其进行迭代时,您隐式地将字符串转换为 char[]
并对其进行迭代。
将流加载到 XDocument
并使用 Linq 获取值:
try
{
var url = ConfigurationManager.AppSettings["url"] + string.Format("/ws/RetailerBrandsFiltered?postcode={0}&countryCode={1}", model.Postcode, model.SelectedCountry);
// Create the request to call the external mvc controller to retrieve the retailers
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "xml";
// Do the request to external controller
var xDoc = XDocument.Load(req.GetResponse().GetResponseStream());
// Get some nodes
var items = xDoc.Descendants("item");
foreach(var item in items)
{
string urn = item.Element("URN").Single().Value;
// Do more stuff
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
请参阅 MSDN article and this Stack Overflow question 了解更多详情。