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.
任何想法?
感谢
你在哪一行发生错误? –
@inquisitiveIdiot当我尝试读取元素时,它从label13.Text = dm.Element(“category”)开始。和其他标签。谢谢 – user3733885
你确定doc.Descendants(“prodcutinfo”)有什么吗? –