• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Look if a certain attribuut of a xml-file exists.

Hi,

I have wrote this code that I have put in the code-section.

I get a error "Object reference not set to an instance of an object."

at line: "shortInf = node.Attributes["shortinfo"].Value;"

I think because I get an error is I have to look if that certain
attribuut is there before reading it!

How can I do that?


Greetings,

Peter Kiers


private void btnEvent_Click(object sender, EventArgs e)
        {
            string[] captions = { "Day", "Date", "Time", "Short Info", "Description" };
            for (int i = 0; i < 5; i++)
            {
                lvInsul.Columns.Add();
                lvInsul.Columns[i].Caption = captions[i];
                lvInsul.Columns[i].VisibleIndex = i;
                ReadEvent();
            }
        }

private void ReadEvent()
        {
            XmlNodeList LoopNodes;
            DateTime theDate4;
            string theTime4;
            string shortInf;
            string Descript;
            TreeListNode RootNode = null;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XML\\I0107448.XML");
                LoopNodes = doc.GetElementsByTagName("EVENT");
                lvInsul.BeginUnboundLoad();
                lvInsul.ClearNodes();
                foreach (XmlNode node in LoopNodes)
                {
                    theDate4 = DateTime.Parse(node.Attributes["Dt"].Value);
                    theTime4 = node.Attributes["Tm"].Value;
                    shortInf = node.Attributes["shortinfo"].Value;
                    Descript = node.Attributes["description"].Value;
                    TreeListNode rootNode = lvInsul.AppendNode(new object[] { theDate4.DayOfWeek.ToString(), 
                    theDate4.ToString("dd-MM-yyyy"), theTime4, shortInf, Descript}, RootNode);
                }
            }
            catch (XmlException)
            {
            }
            finally
            {
                lvInsul.EndUnboundLoad();
            }

        }

Open in new window

0
peterkiers
Asked:
peterkiers
  • 2
1 Solution
 
käµfm³d 👽Commented:
Change your line into these lines:

if (node.Attributes["shortinfo"] != null)
{
    shortInf = node.Attributes["shortinfo"].Value;
}

Open in new window

0
 
peterkiersAuthor Commented:
I get: "Use of unassigned local variable 'shortInf' "

at line:

                  TreeListNode rootNode = lvInsul.AppendNode(new object[] { theDate4.DayOfWeek.ToString(),
                    theDate4.ToString("dd-MM-yyyy"), theTime4, shortInf, Descript}, RootNode);       <===================


Peter
0
 
peterkiersAuthor Commented:
Got it: string shortInf = null;
P.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now