0

Am trying to loop through xmlnodes. but getting this error "Object reference not set to an instance of an object." My main goal was to loop it through the datgridviewcell but i don't know how i can take it further. I know the column indexes for font,date and comment from my grid. how can i loop through those column indexes and be able to parse the value to string?

 <?xml version="1.0" encoding="utf-8"?>
 <root>
<data name="Button"xml:space="preserve">
<value></value>
<comment>[Font][/Font][DateStamp][/DateStamp[Comment][/Comment]</comment>
 </data>

     XmlNodeList _Nodelist = _doc.SelectNodes("/root");
            foreach(XmlNode _Xnode in _Nodelist)
            {
                XmlNode _data = _Xnode.SelectSingleNode("data");
                XmlNodeList _CommentNodes = _data.SelectNodes("comment");
                if(_CommentNodes != null)
                {
                    foreach(XmlNode node in _CommentNodes)
                    {
                        XmlNode _comment = node.SelectSingleNode("comment");
                        {

                                string _font = _comment["Font"].InnerText; //it throws the error here
                                string _Date = _comment["DateStamp"].InnerText;
                                string _Comment = _comment["Comment"].InnerText;
                        }
                    }
                }
            }
IT Forward
  • 367
  • 2
  • 7
  • 28
  • Not sure what the square brackets are for, but [/DateStamp is missing a closing one... – komodosp Feb 25 '15 at 10:53
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ant P Feb 25 '15 at 14:10

1 Answers1

0

Your problems are that

  1. There is only one level of <comment> node, but you are looking for nested comment nodes.
  2. The text inside the <comment> node isn't XML - it's just text content. Thus it cannot be parsed as if it contained child XML elements.

Instead you need to do something like the following:

        XmlNodeList _Nodelist = _doc.SelectNodes("/root");
        foreach (XmlNode root in _Nodelist)
        {
            XmlNode _data = root.SelectSingleNode("data");
            if (_data == null)
                continue;
            XmlNodeList _CommentNodes = _data.SelectNodes("comment");
            foreach (XmlNode _comment in _CommentNodes)
            {
                var text = _comment.InnerText;
                // text = "[Font][/Font][DateStamp][/DateStamp[Comment][/Comment]"
                string _font = text.Between("[Font]", "[/Font]", StringComparison.Ordinal);
                string _Date = text.Between("[DateStamp]", "[/DateStamp]", StringComparison.Ordinal);
                string _Comment = text.Between("[Comment]", "[/Comment]", StringComparison.Ordinal);
            }
        }

Using the extension method:

public static class TextHelper
{
    public static string Between(this string input, string start, string end, StringComparison comparison)
    {
        var startIndex = input.IndexOf(start, comparison);
        if (startIndex < 0)
            return null;
        startIndex += start.Length;
        var endIndex = input.IndexOf(end, startIndex, comparison);
        if (endIndex < 0)
            return null;
        return input.Substring(startIndex, endIndex - startIndex);
    }
}

Note that _Date will be null because, as @colmde noted, "[/DateStamp" is missing a closing bracket.

dbc
  • 104,963
  • 20
  • 228
  • 340