当我试图在xml中获取节点列表时获取空引用异常

时间:2014-05-15 02:33:10

标签: c# xml json c#-4.0 nullreferenceexception

我有以下json格式,我将其转换为xml格式。因为xml格式不正确。我正在更改为正确的格式。现在当我尝试读取值时,我将获得null引用exception.where i我在做错吗?

{
  "category": {
    "1": {
      "wpseo_title": "Makeup & Beauty Tips, Looks, Tutorials and Trends -Makeup.com",
      "wpseo_desc": "Discover all of the latest makeup and beauty looks, trends, styles and new products on Makeup.com! Make up and beauty tips and tricks for the biggest beauty trends.",
      "wpseo_noindex": "default",
      "wpseo_sitemap_include": "-"
    },
    "4": {
      "wpseo_title": "Face & Skin Care - Face Makeup, Skin Care, & Sun Care Tips - Makeup.com",
      "wpseo_desc": "How to and videos for face makeup application, skincare, and sun care. Beauty bloggers, celebrity stylists & makeup artists share all their tips, tricks, and favorite products.",
      "wpseo_noindex": "default",
      "wpseo_sitemap_include": "-"
    }
  }
}

转换为xml后,我得到以下格式。

<category><1><wpseo_title>Makeup &amp; Beauty Tips, Looks, Tutorials and Trends -Makeup.com</wpseo_title><wpseo_desc>Discover all of the latest makeup and beauty looks, trends, styles and new products on Makeup.com! Make up and beauty tips and tricks for the biggest beauty trends.</wpseo_desc><wpseo_noindex>default</wpseo_noindex><wpseo_sitemap_include>-</wpseo_sitemap_include></1><4><wpseo_title>Face &amp; Skin Care - Face Makeup, Skin Care, &amp; Sun Care Tips - Makeup.com</wpseo_title><wpseo_desc>How to and videos for face makeup application, skincare, and sun care. Beauty bloggers, celebrity stylists &amp; makeup artists share all their tips, tricks, and favorite products.</wpseo_desc><wpseo_noindex>default</wpseo_noindex><wpseo_sitemap_include>-</wpseo_sitemap_include></4><5><wpseo_title>Eye Makeup and Eyebrow How To and Videos - Makeup.com</wpseo_title><wpseo_desc>How to and videos for eye makeup, eyebrows, &amp; eye lashes. Application tips on trendy eye looks, liquid liners, smoky eyes, cat eyes, false lashes, mascara, &amp; bold brows.</wpseo_desc><wpseo_noindex>default</wpseo_noindex><wpseo_sitemap_include>-</wpseo_sitemap_include></5></category>

我正在做的是转换&lt; 1&gt;数字格式如下。

这是我的完整代码。

 class Program
        {
            static void Main(string[] args)
            {
                string ReadJosnParameters = File.ReadAllText(@"C:\Users\Elcot\Desktop\TestPackage\TestPackage\SampleJson.json");
                XmlDocument doc = JsonConvert.DeserializeXmlNode(ReadJosnParameters);
                string[] CategoryIds = new string[] {"1","4","5"};
                string xml =string.Empty;
                string replacestr = doc.InnerXml.Replace("category", "categories");
                foreach (string categoryid in CategoryIds)
                {
                    replacestr = replacestr.Replace("<" + categoryid + ">", "<Category id=\"" + categoryid + "\">").
                                                                         Replace("</" + categoryid + ">", "</Category>");
                }
                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(replacestr);
                foreach (string categoryid in CategoryIds)
                {
//This is where xnList is null.
                    XmlNode xnList = xdoc.SelectSingleNode("/categories/category[@id=\'" + categoryid + "\']");
                    foreach (XmlNode xn in xnList)
                    {
                        Console.WriteLine("Seo title {0}", xn["wpseo_title"].InnerText);
                        Console.WriteLine("Seo desc {0}", xn["wpseo_desc"].InnerText);
                    }
                }
                Console.ReadKey();
            }
        }

0 个答案:

没有答案