XML XPath Question

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

Mark Fox

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

Bret Mulvey [MS]

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;
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top