XML XPath Question

Discussion in 'ASP .Net' started by Mark Fox, Nov 15, 2003.

  1. Mark Fox

    Mark Fox Guest

    Hello,

    I have some XML loaded into an XmlDocument and
    attempting to figure out how to access certain nodes. I
    assume I should use XPath, but I am having trouble
    getting the XPath syntax correct. Where might I be going
    wrong? Any help would be appreciated!

    XML:

    <Reason>

    <ReasonCategory>Death</ReasonCategory>
    <Contacts>
    <Contact>
    <Name></Name>
    <Phone></Phone>

    <Address></Address>

    <CityStateZip></CityStateZip>

    <Relation></Relation>

    <IsMember></IsMember>
    </Contact>
    </Contacts>
    <ReasonDetails>

    <PlaceOfDeath></PlaceOfDeath>

    <CauseOfDeath></CauseOfDeath>

    <FuneralLocationName></FuneralLocationName>

    <FuneralDateTime></FuneralDateTime>

    <FuneralFoodRequirements></FuneralFoodRequirements
    >

    <ReasonDetails>
    <RequestedServices>
    <RequestedService>

    <OfferedServiceName></OfferedServiceName>

    <AssignedTo></AssignedTo>
    </RequestedService>
    </RequestedServices>
    <FormDetails>

    <CompletedBy></CompletedBy>

    <CompletedDate></CompletedDate>
    <Notes></Notes>
    </FormDetails>
    </Reason>

    I am attempting to access the ReasonCategory node with:

    XmlNodeReader myXmlNodeReader = new XmlNodeReader
    (myXmlDocument.SelectSingleNode("Reason/ReasonCategory"));
    string ReasonCategory =
    myXmlNodeReader.Value;

    But the XmlNodeReader isn't finding the node. Thanks for
    your help!
    Mark Fox, Nov 15, 2003
    #1
    1. Advertising

  2. Mark Fox

    Mark Fox Guest

    Nevermind, I figured it out. Sorry!


    >-----Original Message-----
    >Hello,
    >
    > I have some XML loaded into an XmlDocument and
    >attempting to figure out how to access certain nodes. I
    >assume I should use XPath, but I am having trouble
    >getting the XPath syntax correct. Where might I be

    going
    >wrong? Any help would be appreciated!
    >
    >XML:
    >
    > <Reason>
    >
    > <ReasonCategory>Death</ReasonCategory>
    > <Contacts>
    > <Contact>
    > <Name></Name>
    > <Phone></Phone>
    >
    > <Address></Address>
    >
    > <CityStateZip></CityStateZip>
    >
    > <Relation></Relation>
    >
    > <IsMember></IsMember>
    > </Contact>
    > </Contacts>
    > <ReasonDetails>
    >
    > <PlaceOfDeath></PlaceOfDeath>
    >
    > <CauseOfDeath></CauseOfDeath>
    >
    > <FuneralLocationName></FuneralLocationName>
    >
    > <FuneralDateTime></FuneralDateTime>
    >
    > <FuneralFoodRequirements></FuneralFoodRequirements
    >>

    > <ReasonDetails>
    > <RequestedServices>
    > <RequestedService>
    >
    > <OfferedServiceName></OfferedServiceName>
    >
    > <AssignedTo></AssignedTo>
    > </RequestedService>
    > </RequestedServices>
    > <FormDetails>
    >
    > <CompletedBy></CompletedBy>
    >
    > <CompletedDate></CompletedDate>
    > <Notes></Notes>
    > </FormDetails>
    > </Reason>
    >
    >I am attempting to access the ReasonCategory node with:
    >
    >XmlNodeReader myXmlNodeReader = new XmlNodeReader
    >(myXmlDocument.SelectSingleNode

    ("Reason/ReasonCategory"));
    > string ReasonCategory =
    >myXmlNodeReader.Value;
    >
    >But the XmlNodeReader isn't finding the node. Thanks

    for
    >your help!
    >.
    >
    Mark Fox, Nov 15, 2003
    #2
    1. Advertising

  3. "Mark Fox" <> wrote in message
    news:062901c3ab10$8e7ae130$...
    > Hello,
    >
    > I have some XML loaded into an XmlDocument and
    > attempting to figure out how to access certain nodes. I
    > assume I should use XPath, but I am having trouble
    > getting the XPath syntax correct. Where might I be going
    > wrong? Any help would be appreciated!
    >
    > XML:
    >
    > <Reason>
    >
    > <ReasonCategory>Death</ReasonCategory>

    etc.
    > </Reason>
    >
    > I am attempting to access the ReasonCategory node with:
    >
    > XmlNodeReader myXmlNodeReader = new XmlNodeReader
    > (myXmlDocument.SelectSingleNode("Reason/ReasonCategory"));
    > string ReasonCategory =
    > myXmlNodeReader.Value;
    >
    > But the XmlNodeReader isn't finding the node. Thanks for
    > your help!


    The "Value" of an XmlElement node is null. What you want is the InnerText.
    And instead of using XmlNodeReader you can also use the SelectSingleNode
    result directly, e.g.

    string ReasonCategory =
    myXmlDocument.SelectSingleNode("/Reason/ReasonCategory").InnerText;
    Bret Mulvey [MS], Nov 15, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,953
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,364
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    505
  4. goog
    Replies:
    0
    Views:
    492
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,606
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page