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 & 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 & Skin Care - Face Makeup, Skin Care, & 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 & 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, & eye lashes. Application tips on trendy eye looks, liquid liners, smoky eyes, cat eyes, false lashes, mascara, & 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();
}
}