XML XPath Question

  • Thread starter Thread starter Mark Fox
  • Start date Start date
M

Mark Fox

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>

<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!
 
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>


<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 said:
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;
 
Back
Top