【问题标题】:C# XML XPATH cant assign data to an array to show it in ListViewC# XML XPATH 无法将数据分配给数组以在 ListView 中显示
【发布时间】:2012-11-17 20:54:18
【问题描述】:

我是 C# 和 XML 数据用法的新手。

我有以下 xml 数据。 此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。

<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>

我使用那个 C# 代码。 (这是Form1使用的类)

    public IXmlNamespaceResolver ns { get; set; }    

public string[] user,id,title,value,period;
    public void XmlRead(string url)
    {
                // Create a new XmlDocument  
                XPathDocument doc = new XPathDocument(url);
                // Create navigator  
                XPathNavigator navigator = doc.CreateNavigator();
                // Get forecast with XPath  
                XPathNodeIterator nodes = navigator.Select("/response/auctions", ns);

                int i = 0;
                foreach (XPathNavigator oCurrentPerson in nodes)
                {   
                    userName[i] = oCurrentPerson.SelectSingleNode("user").Value;
                    userId[i] = int.Parse(oCurrentPerson.SelectSingleNode("id").Value);
                    title[i] = oCurrentPerson.SelectSingleNode("title").Value;
                    value[i] = oCurrentPerson.SelectSingleNode("value").Value;
                    period[i] = oCurrentPerson.SelectSingleNode("period").Value;
                    i++; }
    }

我收到一个错误: 对象引用未设置为对象的实例。

userName[i] = oCurrentPerson.SelectSingleNode("user").Value;

当我使用单个字符串变量时:userName、userId 没有 [] 一切正常。

提前致谢

【问题讨论】:

    标签: c# xml arrays xpath


    【解决方案1】:

    .Net 是一个强类型的世界,所以使用它的好处。创建Auction 类来保存您的xml 中的数据:

    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# 数据一起使用。但我猜这是完成它的更好、更简单的方法。
    【解决方案2】:
    navigator.Select("/response/auctions/auction", ns);
    

    【讨论】:

      猜你喜欢
      • 2017-08-09
      • 2019-05-31
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多