0

I have following json format where i am converting this into xml format.since the xml is not in proper format.i am changing to proper format.now when i try to read the value i am getting null reference exception.where i am doing the mistake?

{
  "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": "-"
    }
  }
}

after converting to xml,i am getting the following format.

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

what i am doing is convert <1> numbers into following format.

Here is my complete code.

 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();
            }
        }
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 02:34
  • found the solution guys.Case sensitive issue in the above code.Thanks for your comments.Got some additional information from the above link and it's nice.Thanks John Saunders – user3343557 May 15 '14 at 03:01
  • Why reopen this? The OP got the answer from reading the linked duplicate. – John Saunders May 15 '14 at 03:02
  • Sorry John.I am new to Stackoverflow.Moreover the previous questions was not asked by me. – user3343557 May 15 '14 at 03:06
  • I wasn't referring to you, unless you're the one who voted to reopen? – John Saunders May 15 '14 at 03:07

0 Answers0