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

LVL 1
Peter KiersOperatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KiersOperatorAuthor 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
Peter KiersOperatorAuthor Commented:
Got it: string shortInf = null;
P.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.