如何将xml传递给C#中的View?
Posted
技术标签:
【中文标题】如何将xml传递给C#中的View?【英文标题】:How to pass xml to View in C#? 【发布时间】:2020-11-02 13:02:50 【问题描述】:它从请求中以字符串形式提供 xml。反序列化后,我无法使用 get
方法从 xml 中获取任何值。同样在Vehicle.cshtml
中,我试图获得DeviceId
的值。它仅将字符串名称 DeviceId
获取到 html 标签。得不到价值。我的方式有什么问题吗?如何从视图中获取这些 xml 值?
DataSet.cs
using System;
using System.Xml.Serialization;
namespace ShippingProject.Models
// [System.Xml.Serialization.XmlRootAttribute("DataSet", Namespace = "http://www.arvento.com/", IsNullable = false)]
[XmlRoot("DataSet")]
public class DataSet
[XmlElement("Device_x0020_No")]
public string DeviceId get; set;
[XmlElement("Latitude")]
public double Latitude get; set;
[XmlElement("Longitude")]
public double Longitude get; set;
[XmlElement("Address")]
public string Address get; set;
HomeController.cs
public IActionResult Vehicle()
// Create web client.
WebClient client = new WebClient();
// Download string.
string value = client.DownloadString("http://ws.arvento.com//v1/report.asmx/GetVehicleStatus?Username=username&PIN1=password&PIN2=password&Language=string");
// Write values.
Console.WriteLine("--- WebClient result ---");
Console.WriteLine(value.Length);
Console.WriteLine(value);
XmlSerializer serializer = new XmlSerializer(typeof(List<DataSet>), new XmlRootAttribute("DataSet"));
// DataSet result;
var ms = new MemoryStream(Encoding.UTF8.GetBytes(value));
var rdr = new XmlTextReader(ms) Namespaces = false ;
// StringReader rdr = new StringReader(value);
/* using (TextReader rdr = new StringReader(value))
result = (DataSet)serializer.Deserialize(reader);
StreamReader rdr = new StreamReader(value);*/
List<DataSet> datasetList = (List<DataSet>)serializer.Deserialize(rdr);
Console.WriteLine("--- Deserialize result ---");
Console.WriteLine(datasetList);
datasetList.ForEach(dl =>
Console.WriteLine(dl.DeviceId);
Console.WriteLine(dl.Latitude);
Console.WriteLine(dl.Longitude);
Console.WriteLine(dl.Address);
Console.WriteLine();
);
return View();
Vehicle.cshtml
@model DataSet
@Html.LabelFor(model => model.DeviceId)
这是我的 xml 文件:
<DataSet xmlns="http://www.arvento.com/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="tblVehicleStatus">
<xs:element name="tblVehicleStatus" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="dtVehicleStatus">
<xs:complexType>
<xs:sequence>
<xs:element name="Device_x0020_No" type="xs:string" minOccurs="0"/>
<xs:element name="GMT_x0020_Date_x002F_Time" type="xs:dateTime" minOccurs="0"/>
<xs:element name="Latitude" type="xs:double" minOccurs="0"/>
<xs:element name="Longitude" type="xs:double" minOccurs="0"/>
<xs:element name="Speed" type="xs:double" minOccurs="0"/>
<xs:element name="Address" type="xs:string" minOccurs="0"/>
<xs:element name="Building_x0020__x002F__x0020_Region" type="xs:string" minOccurs="0"/>
<xs:element name="Height" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<tblVehicleStatus xmlns="">
<dtVehicleStatus diffgr:id="dtVehicleStatus1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<Device_x0020_No>805251</Device_x0020_No>
<GMT_x0020_Date_x002F_Time>2020-07-10T06:00:55+03:00</GMT_x0020_Date_x002F_Time>
<Latitude>41.070751</Latitude>
<Longitude>29.000586</Longitude>
<Speed>0</Speed>
<Address>Cemal Sururi Sk., Gülbahar Mh., Şişli, İstanbul, Türkiye</Address>
<Building_x0020__x002F__x0020_Region/>
<Height>205</Height>
</dtVehicleStatus>
<dtVehicleStatus diffgr:id="dtVehicleStatus2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<Device_x0020_No>805252</Device_x0020_No>
<GMT_x0020_Date_x002F_Time>2020-07-08T13:33:24+03:00</GMT_x0020_Date_x002F_Time>
<Latitude>41.071037</Latitude>
<Longitude>29.00054</Longitude>
<Speed>7.86</Speed>
<Address>Şat Otopark, Çamlı Sk., Gülbahar Mh., Şişli, İstanbul, Türkiye</Address>
<Building_x0020__x002F__x0020_Region/>
<Height>151</Height>
</dtVehicleStatus>
<dtVehicleStatus diffgr:id="dtVehicleStatus3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
<Device_x0020_No>805253</Device_x0020_No>
<GMT_x0020_Date_x002F_Time>2020-07-10T05:31:43+03:00</GMT_x0020_Date_x002F_Time>
<Latitude>41.071087</Latitude>
<Longitude>29.000429</Longitude>
<Speed>6.31</Speed>
<Address>Şat Otopark, Çamlı Sk., Gülbahar Mh., Şişli, İstanbul, Türkiye</Address>
<Building_x0020__x002F__x0020_Region/>
<Height>103</Height>
</dtVehicleStatus>
</tblVehicleStatus>
</diffgr:diffgram>
</DataSet>
【问题讨论】:
您不能序列化必须在根目录 (ListDataSet
替换所有List<DataSet>
吗?
我需要查看 xml 样本才能给出准确的答案。我现在只能说 List 不起作用。
【参考方案1】:
尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication8
class Program
const string FILENAME = @"C:\temp\test.xml";
static void Main(string[] args)
string xml = File.ReadAllText(FILENAME);
StringReader sReader = new StringReader(xml);
XmlReader reader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataSet));
ConsoleApplication8.DataSet dataset = (ConsoleApplication8.DataSet)serializer.Deserialize(reader);
public class Diffgram
[XmlArray(ElementName = "tblVehicleStatus", Namespace = "")]
[XmlArrayItem(ElementName = "dtVehicleStatus", Namespace = "")]
public List<DtVehicleStatus> DtVehicleStatus get; set;
public class DtVehicleStatus
[XmlElement("Device No")]
public string DeviceId get; set;
[XmlElement("Latitude")]
public double Latitude get; set;
[XmlElement("Longitude")]
public double Longitude get; set;
[XmlElement("Address")]
public string Address get; set;
[XmlRoot(ElementName = "DataSet", Namespace = "http://www.arvento.com/")]
public class DataSet
[XmlElement(ElementName = "diffgram", Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1")]
public Diffgram Diffgram get; set;
【讨论】:
感谢您的帮助。我将在我的项目中添加此代码。你能写这个吗。当我在 HomeController 中添加它时。命名空间产生问题。 用我的课代替你的课。然后你只需要修改两行来匹配我的代码 1) XmlSerializer serializer = new XmlSerializer(typeof(List以上是关于如何将xml传递给C#中的View?的主要内容,如果未能解决你的问题,请参考以下文章