我需要访问一个URL并获取xml响应(标记中包含冒号),并将响应绑定到模型类中,并将其显示在html表中。
到目前为止,我已经访问了url,阅读了xml,我的问题是我无法绑定模型类中的数据,因为标签中有冒号,没有冒号我就可以轻松地做到,我已经阅读了使用名称空间是一种方法,但我对此并不陌生,无法实现逻辑。任何帮助将非常感激。
P.S:我正在使用mvc,c#,剃刀视图
//this will read the url and get back xml response and save it
string xml = null;
WebRequest req = WebRequest.Create("my__xml__link__url");
req.Credentials = CredentialCache.DefaultCredentials;
WebResponse res = req.GetResponse();
Stream dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
xml = reader.ReadToEnd();
reader.Close();
res.Close();
int length = 8;
const string valid =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder resz = new StringBuilder();
Random rnd = new Random();
while (0 < length--)
{
resz.Append(valid[rnd.Next(valid.Length)]);
}
string sf = DateTime.Now.ToString();
string generatedresult = resz.ToString() + "_" + ".xml";
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(xml);
}
catch (Exception ex)
{
string dgj = ex.ToString();
}
doc.Save(@"D:\" + generatedresult + "");
//this below code to read the data and bind it in the model and
//displayit in the html table
List<CustomerModel> customers = new List<CustomerModel>();
//Load the XML file in XmlDocument.
doc.Load(Server.MapPath("~/oWtMRUR8_.xml"));
foreach (XmlNode node in
doc.SelectNodes("/m:propertiesz/m:properties"))
{
customers.Add(new CustomerModel
{
CustomerId = int.Parse(node["d:No"].InnerText),
Name = node["d:Description"].InnerText,
Country = node["d:Type"].InnerText
});
}
return View(customers);
// I have attached my sample xml code
<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="" xmlns="http://www.w3.org/2005/Atom" xmlns:d=""
xmlns:m="">
<id></id>
<title type="text">ItemList</title>
<updated>2019-05-08T12:10:04Z</updated>
<link rel="self" title="ItemList" href="ItemList" />
<id></id>
<category term="NAV.ItemList" scheme="" />
<link rel="edit" title="ItemList" href="" />
<title />
<updated>2019-05-08T12:10:04Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:No>1000</d:No>
<d:Description>Bicycle</d:Description>
<d:Type>Inventory</d:Type>
</m:properties>
</content>
<id></id>
<category term="NAV.ItemList" scheme="" />
<link rel="edit" title="ItemList" href="" />
<title />
<updated>2019-05-08T12:10:04Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:No>1001</d:No>
<d:Description>Touring Bicycle</d:Description>
<d:Type>Inventory</d:Type>
</m:properties>
</content>
我只需要提取d:标记内的内容。 注意:由于隐私原因,我对示例xml内容进行了稍微的编辑