XmlDocument persistent errors

  • Thread starter Thread starter Philip Townsend
  • Start date Start date
P

Philip Townsend

I am attempting to parse through an XML document using the XmlDocument
object. I keep getting a NullReferenceException. I have even tried using
the fully qualified name, but with no luck. Here is the code:

private void Page_Load(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();

System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(Server.MapPath("lib/schoolAnnouncements.xml"));
XmlNode nd;

nd = xd.FirstChild["counties"];

foreach(System.Xml.XmlNode n in nd.ChildNodes)
{
if(n.Name=="county")
sb.Append(String.Format("{0}<br>",n.InnerText));
}

elems=sb.ToString();
}

Any suggestions?
 
Consider the syntax you used in:

nd = xd.FirstChild["counties"];

Did you mean to use this instead?

nd = xd.SelectNode("counties");

FirstChild is a property of a node, but you're using it as if it were an
indexed collection.
 
Back
Top