2014-07-01 59 views
0

首先,这不是一个重复的问题,我已经搜索和搜索,找不到我在找什么。XML读取错误(C#)

继承人我的问题,我想通读一个XML文档并将值存储在我的winforms应用程序中的标签中,我设法在同一个应用程序中这样做,但这只是不工作.. 。

继承人的XML文档

<Root> 
    <productinfo> 
    <id>7698011</id> 
    <productinfo>WP Online Contract</productinfo> 
    <url>LINK_URL_REMOVED</url> 
    <user>futuredesigngrp</user> 
    <thumbnail>LINK_URL_REMOVED</thumbnail> 
    <sales>73</sales> 
    <rating>5</rating> 
    <rating_decimal>5.00</rating_decimal> 
    <cost>16.00</cost> 
    <uploaded_on>Tue May 13 16:13:43 +1000 2014</uploaded_on> 
    <last_update>Sun Jun 29 18:58:15 +1000 2014</last_update> 
    <tags>client, contract, create, edit, mobile, online, plugin, responsive, sign, sign online, signature, upload, wordpress</tags> 
    <category>wordpress/utilities</category> 
    <live_preview_url>LINK_URL_REMOVED</live_preview_url> 
    </productinfo> 
</Root> 

这里是我的代码:

  productID = Properties.Settings.Default.WorkingID; 
      productSite = Properties.Settings.Default.ProductSite; 
      this.Text = this.Text + " :: Viewing Product ID #" + Properties.Settings.Default.WorkingID; 

      WebClient client = new WebClient(); 
      string productDetails = client.DownloadString("http://marketplace.envato.com/api/v3/item:" + productID + ".json"); 

      XNode node = JsonConvert.DeserializeXNode(productDetails, "Root"); 
      System.IO.File.WriteAllText(@"productDetails_" + productID + ".xml", node.ToString()); 

      var fileContents = System.IO.File.ReadAllText(@"productDetails_" + productID + ".xml"); 
      fileContents = fileContents.Replace("<item>", "<productinfo>"); 
      fileContents = fileContents.Replace("</item>", "</productinfo>"); 
      System.IO.File.WriteAllText(@"productDetails_" + productID + ".xml", fileContents); 

      XDocument doc = XDocument.Load("productDetails_" + productID + ".xml"); 

      foreach (var dm in doc.Descendants("productinfo")) 
      { 
       //label11.Text = dm.Element("id").Value; 
       //label12.Text = dm.Element("productinfo").Value; 
       label13.Text = dm.Element("category").Value; 
       label14.Text = dm.Element("url").Value; 
       label15.Text = dm.Element("user").Value; 
       label16.Text = dm.Element("sales").Value; 
       label17.Text = dm.Element("rating_decimal").Value; 
       label18.Text = "$" + dm.Element("cost").Value; 
       label19.Text = dm.Element("uploaded_on").Value; 
       label20.Text = dm.Element("last_update").Value; 
      } 

然而,当我运行代码,我得到了以下错误:

Object reference not set to an instance of an object. 

任何想法?

感谢

+1

你在哪一行发生错误? –

+0

@inquisitiveIdiot当我尝试读取元素时,它从label13.Text = dm.Element(“category”)开始。和其他标签。谢谢 – user3733885

+0

你确定doc.Descendants(“prodcutinfo”)有什么吗? –

回答

0

的问题是可重复的,如果XML数据不包含一些属性(例如“类别”)。我看到您的xml数据包含嵌套的“productinfo”标签,因此表达式 dm.Element("category")返回null,并在处理嵌套的“productinfo”标签时产生异常。

您可以跳过嵌套“productinfo”标签处理:

foreach(var dm in doc.Descendants("productinfo")) { 
    if(!dm.HasElements) return;; 
    //... 

或只是删除/重命名嵌套“productinfo”标签,或者使用下面的改进访问XElement.Value属性:

string category = dm.Element("category").GetValueOrDefault("Uncategorized"); 
string url = dm.Element("url").GetValueOrDefault(); 
//... 


public static class XElementExtension { 
    // GetValueOrDefault extension for XElement 
    public static string GetValueOrDefault(this XElement element, string defaultValue = null) { 
     return (element != null) ? element.Value : defaultValue; 
    } 
}