简体   繁体   中英

Xml reading through c# null reference exception

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace Read_Attribute
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"C:\\Users\\Sunny\\Documents\\Visual Studio 2012\\Projects\\taskl\\taskl\\bin\\Debug\\procy.xml";
            XmlDocument xml = new XmlDocument();
            using (XmlTextReader tr = new XmlTextReader(path))
            {
                tr.Namespaces = false;
                xml.Load(tr);
            }
            XmlNodeList list = xml.GetElementsByTagName("omgdc:Bounds");
            XmlNodeList listt = xml.GetElementsByTagName("task");
            bool flag = true;
            bool flag1 = true;

            Double x1 = 0, x2 = 0, y1 = 0, y2 = 0;

            string c1 = null;
            string c2 = null;
            string v1 = null;
            string v2 = null;

            foreach (XmlNode m in listt)
            {
                if (m.Attributes.Count > 0)
                {
                    if (flag1)
                    {
                        c1 = (m.Attributes["id"].InnerText);
                        v1 = (m.Attributes["id"].InnerText);
                        flag1 = false;
                        Console.WriteLine("The id of task is :" + c1, "The id of task is :" + v1);
                    }
                    else
                    {
                        c2 = (m.Attributes["id"].InnerText);
                        v2 = (m.Attributes["id"].InnerText);
                        flag1 = true;

                        Console.WriteLine("The id of task is :" + c2, "The id of task is :" + v2);
                    }
                }
            }

            foreach (XmlNode n in list)
            {
                String idStr = n.Attributes["id"].InnerText;    
            if ((idStr != null) && ((idStr == c1) || (idStr == v1) || (idStr == c2) || (idStr == v2)))

                    {
                        if (flag)
                        {
                            x1 = Convert.ToDouble(n.Attributes["x"].InnerText);
                            y1 = Convert.ToDouble(n.Attributes["y"].InnerText);
                            flag = false;
                            Console.WriteLine("The dimension of x is :" + x1);
                            Console.WriteLine("The dimension of y is :" + y1);

                        }
                        else
                        {
                            x2 = Convert.ToDouble(n.Attributes["x"].InnerText);
                            y2 = Convert.ToDouble(n.Attributes["y"].InnerText);
                            flag = true;

                            if (x2 > x1)
                            {
                                Console.WriteLine("The tasks don't overlap with each other");

                            }
                            else
                                Console.WriteLine("The tasks overlap with each other");
                        }
                    }
                }
            }
        }
    }

String idStr = n.Attributes["id"].InnerText there was a null reference exception at this point how to remove this error i want to read XML through this code of a business process model kindly help me in removing the error as soon as possible Thankx in advance for ur help

Its difficult to identify exact error without knowing what XML looks like. But there are few correction which may work for null reference.

1) Is the path correct?

String path = @"C:\\Users\\Sunny\\Documents\\Visual Studio 2012\\Projects\\taskl\\taskl\\bin\\Debug\\procy.xml";

Any particular reason for extra escape character even when using @ ? I believe it should be like

String path = @"C:\Users\Sunny\Documents\Visual Studio 2012\Projects\taskl\taskl\bin\Debug\procy.xml";

Which means first check is, whether xml is actually getting loaded?

2) Add a check for Attributes :

Something like

if (n.Attributes["id"] != null)

3) Sure you need InnerText or is it value instead?

Perhaps try this

String attributeValue = n.Attributes["id"].Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM