读取从navison收到的xml响应,并将响应绑定到mvc模型中,并显示在表中
我需要访问一个url并获取xml响应(标记中包含冒号),然后将响应绑定到模型类中,并将其显示在html表中 到目前为止,我已经访问了url,阅读了xml,我的问题是我无法绑定模型类中的数据,因为标记中有冒号,没有冒号我可以轻松地进行绑定,我已经了解到使用名称空间是一种方式,但我对这种方式不熟悉,无法实现逻辑。任何帮助都将非常感谢 附言:我正在使用mvc、c#、razor视图读取从navison收到的xml响应,并将响应绑定到mvc模型中,并显示在表中,xml,asp.net-mvc,xml-parsing,navision,Xml,Asp.net Mvc,Xml Parsing,Navision,我需要访问一个url并获取xml响应(标记中包含冒号),然后将响应绑定到模型类中,并将其显示在html表中 到目前为止,我已经访问了url,阅读了xml,我的问题是我无法绑定模型类中的数据,因为标记中有冒号,没有冒号我可以轻松地进行绑定,我已经了解到使用名称空间是一种方式,但我对这种方式不熟悉,无法实现逻辑。任何帮助都将非常感谢 附言:我正在使用mvc、c#、razor视图 //this will read the url and get back xml response
//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>
//这将读取url并返回xml响应并保存它
字符串xml=null;
WebRequest req=WebRequest.Create(“我的xml链接url”);
请求凭证=CredentialCache.DefaultCredentials;
WebResponse res=req.GetResponse();
Stream dataStream=res.GetResponseStream();
StreamReader=新的StreamReader(数据流);
xml=reader.ReadToEnd();
reader.Close();
res.Close();
整数长度=8;
常量字符串有效=
“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ1234567890”;
StringBuilder resz=新的StringBuilder();
随机rnd=新随机();
而(0<长度--)
{
resz.Append(有效[rnd.Next(valid.Length)]);
}
字符串sf=DateTime.Now.ToString();
字符串generatedresult=resz.ToString()+““+”.xml”;
XmlDocument doc=新的XmlDocument();
尝试
{
doc.LoadXml(xml);
}
捕获(例外情况除外)
{
字符串dgj=ex.ToString();
}
文档保存(@“D:\”+generatedresult+”);
//下面的代码用于读取数据并将其绑定到模型和
//在html表中显示它
列出客户=新列表();
//在XmlDocument中加载XML文件。
doc.Load(Server.MapPath(“~/oWtMRUR8_ur8.xml”);
foreach(中的XmlNode节点)
doc.SelectNodes(“/m:propertiesz/m:properties”))
{
customers.Add(新CustomerModel
{
CustomerId=int.Parse(节点[“d:No”].InnerText),
名称=节点[“d:说明”]。InnerText,
国家/地区=节点[“d:类型”]。InnerText
});
}
返回视图(客户);
//我已经附上了我的示例xml代码
线性对象表
2019-05-08T12:10:04Z
2019-05-08T12:10:04Z
1000
自行车
库存
2019-05-08T12:10:04Z
1001
旅游自行车
库存
我只需要提取d:tag中的内容。
注意:由于隐私原因,我对示例xml内容进行了轻微编辑您应该使用的是重载版本。第二个参数应该是名称空间的名称空间管理器