C# C XML XPATH无法将数据分配给数组以在ListView中显示它
我是C和XML数据Usage中的新手 我得到了以下xml数据。 此XML文件似乎没有任何与之关联的样式信息。文档树如下所示C# C XML XPATH无法将数据分配给数组以在ListView中显示它,c#,xml,arrays,xpath,C#,Xml,Arrays,Xpath,我是C和XML数据Usage中的新手 我得到了以下xml数据。 此XML文件似乎没有任何与之关联的样式信息。文档树如下所示 <response> <auctions> <auction> <id>90436</id> <user>blabla</user> <title>title name</title> <value>10000.0
<response>
<auctions>
<auction>
<id>90436</id>
<user>blabla</user>
<title>title name</title>
<value>10000.00</value>
<period>36</period>
<www/>
</auction>
<auction>
<id>90436</id>
<user>blabla</user>
<title>title name</title>
<value>10000.00</value>
<period>36</period>
<www/>
</auction>
</auctions>
</response>
我收到一个错误:
对象引用未设置为对象的实例
userName[i]=oCurrentPerson.SelectSingleNodeuser.Value
当我使用单字符串变量时,比如:userName、userId(不带[]),一切正常
提前感谢.Net是一个强类型的世界,所以请利用它的优势。创建拍卖类以保存xml中的数据:
navigator.Select("/response/auctions/auction", ns);
class Auction
{
public int Id { get; set; }
public string User { get; set; }
public string Title { get; set; }
public decimal Value { get; set; }
public int Period { get; set; }
public string Url { get; set; }
}
并使用Linq to xml解析您的xml:
XDocument xdoc = XDocument.Load(path_to_xml_file);
IEnumerable<Auction> auctions =
from a in xdoc.Descendants("auction")
select new Auction()
{
Id = (int)a.Element("id"),
User = (string)a.Element("user"),
Title = (string)a.Element("title"),
Value = (decimal)a.Element("value"),
Period = (int)a.Element("period"),
Url = (string)a.Element("www")
};
谢谢,我需要学习如何将LINQ与xml c数据一起使用。但我想这是更好更简单的方法来完成它。
XDocument xdoc = XDocument.Load(path_to_xml_file);
IEnumerable<Auction> auctions =
from a in xdoc.Descendants("auction")
select new Auction()
{
Id = (int)a.Element("id"),
User = (string)a.Element("user"),
Title = (string)a.Element("title"),
Value = (decimal)a.Element("value"),
Period = (int)a.Element("period"),
Url = (string)a.Element("www")
};