XmlDocument persistent errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Townsend

    #1

    XmlDocument persistent errors

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

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    StringBuilder sb = new StringBuilder() ;

    System.Xml.XmlD ocument xd = new System.Xml.XmlD ocument();
    xd.Load(Server. MapPath("lib/schoolAnnouncem ents.xml"));
    XmlNode nd;

    nd = xd.FirstChild["counties"];

    foreach(System. Xml.XmlNode n in nd.ChildNodes)
    {
    if(n.Name=="cou nty")
    sb.Append(Strin g.Format("{0}<b r>",n.InnerText ));
    }

    elems=sb.ToStri ng();
    }

    Any suggestions?



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Jerry Houston

    #2
    Re: XmlDocument persistent errors

    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.

    "Philip Townsend" <ptownsend@v1te ch.com> wrote in message
    news:Oq2kpcFtDH A.2340@TK2MSFTN GP12.phx.gbl...[color=blue]
    > I am attempting to parse through an XML document using the XmlDocument
    > object. I keep getting a NullReferenceEx ception. I have even tried using
    > the fully qualified name, but with no luck. Here is the code:
    >
    > private void Page_Load(objec t sender, System.EventArg s e)
    > {
    > StringBuilder sb = new StringBuilder() ;
    >
    > System.Xml.XmlD ocument xd = new System.Xml.XmlD ocument();
    > xd.Load(Server. MapPath("lib/schoolAnnouncem ents.xml"));
    > XmlNode nd;
    >
    > nd = xd.FirstChild["counties"];
    >
    > foreach(System. Xml.XmlNode n in nd.ChildNodes)
    > {
    > if(n.Name=="cou nty")
    > sb.Append(Strin g.Format("{0}<b r>",n.InnerText ));
    > }
    >
    > elems=sb.ToStri ng();
    > }
    >
    > Any suggestions?
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    Comment

    Working...