XmlDocument persistent errors

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?
 
J

Jerry Houston

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top