C# XPath Query

Hi Experts

I have the following code which converts my string to valid XML

XmlDocument xm = new XMLDocument();
xm.loadXml(strMyXML);

Open in new window


Using the xml below I'm trying to capture in a string the refID for the AgedCareAssessmentReferral node where the node contains a specific EpisodeID (1-S3S2 using the xml example below)

I'm not sure how to run the xpath query using c# and also construct the xpath query so any assistance would be appreciated


<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  <soap:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ass:ListReferralsResponse xmlns:ass="http://socialservices.gov.au/ebm/AssessmentReferral">
      <ass:ListReferralsOutput>
        <com:TransactionStatus xmlns:com="http://socialservices.gov.au/Common">
          <com:TransactionStatusCode>Success</com:TransactionStatusCode>
        </com:TransactionStatus>
        <ref:AgedCareAssessmentReferral xmlns:ref="http://socialservices.gov.au/ebo/Referral">
          <ref:ID>1-S4H0</ref:ID>
          <ref:OutletID>1-KA5J</ref:OutletID>
          <ref:Status>Issued</ref:Status>
          <ref:StatusDescription />
          <ref:StatusReason />
          <ref:Comments />
          <ref:Priority>High</ref:Priority>
          <ref:Source>Aged Care Gateway</ref:Source>
          <ref:EpisodeID>1-S3S2</ref:EpisodeID>
          <ref:EpisodeStatus>Active</ref:EpisodeStatus>
          <ref:ScreeningID>1-S3S8</ref:ScreeningID>
          <ref:ScreeningDate>2015-04-16T11:08:40</ref:ScreeningDate>
          <ref:RelatedReferralID />
          <ref:CreatedDate>2015-04-16T11:51:59</ref:CreatedDate>
          <ref:DueDate>2015-04-18T11:51:59</ref:DueDate>
          <ref:RowVersion>2</ref:RowVersion>
          <per:ClientBasic xmlns:per="http://socialservices.gov.au/ebo/Person">
            <per:FirstName>Tori</per:FirstName>
            <per:LastName>Lightswitch</per:LastName>
            <per:ID>1-R753</per:ID>
            <per:MiddleName />
            <per:NickName>G</per:NickName>
            <per:Gender>Male</per:Gender>
            <per:BirthDate>1940-02-04</per:BirthDate>
          </per:ClientBasic>
          <ref:AssessmentType>Comprehensive Assessment</ref:AssessmentType>
        </ref:AgedCareAssessmentReferral>
        <com:PagingControlOutput xmlns:com="http://socialservices.gov.au/Common">
          <com:TotalRecords>1</com:TotalRecords>
        </com:PagingControlOutput>
      </ass:ListReferralsOutput>
    </ass:ListReferralsResponse>
  </soap:Body>
</soap:Envelope>

Open in new window

victoriaharryAsked:
Who is Participating?
 
Ronak PatelCommented:
Oops, my bad.

Please try below code:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);

XmlNamespaceManager xManager = new XmlNamespaceManager(xDoc.NameTable);
xManager.AddNamespace("ref", "http://socialservices.gov.au/ebo/Referral");

XmlNodeList xNodes = xDoc.SelectNodes("//ref:AgedCareAssessmentReferral", xManager);

foreach (XmlNode xNode in xNodes)
{
	XmlDocument xInnerDoc = new XmlDocument();
	xInnerDoc.LoadXml(xNode.OuterXml);

	XmlNode xNodeEpisodeID = xInnerDoc.SelectSingleNode("//ref:EpisodeID", xManager);

	string refID = string.Empty;

	if (xNodeEpisodeID.InnerText.Trim() == "1-S3S2")
		refID = xNode.SelectSingleNode("//ref:ID", xManager).InnerText;
}

Open in new window


Regards,
Ronak
0
 
Ronak PatelCommented:
Hi,

As the xml contains Namespace prefix, u need to add XmlNamespaceManager to your XPath query.

Please refer below code:
string xml = @"<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>
		<soap:Header xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />
		  <soap:Body xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
			<ass:ListReferralsResponse xmlns:ass='http://socialservices.gov.au/ebm/AssessmentReferral'>
			  <ass:ListReferralsOutput>
				<com:TransactionStatus xmlns:com='http://socialservices.gov.au/Common'>
				  <com:TransactionStatusCode>Success</com:TransactionStatusCode>
				</com:TransactionStatus>
				<ref:AgedCareAssessmentReferral xmlns:ref='http://socialservices.gov.au/ebo/Referral'>
				  <ref:ID>1-S4H0</ref:ID>
				  <ref:OutletID>1-KA5J</ref:OutletID>
				  <ref:Status>Issued</ref:Status>
				  <ref:StatusDescription />
				  <ref:StatusReason />
				  <ref:Comments />
				  <ref:Priority>High</ref:Priority>
				  <ref:Source>Aged Care Gateway</ref:Source>
				  <ref:EpisodeID>1-S3S2</ref:EpisodeID>
				  <ref:EpisodeStatus>Active</ref:EpisodeStatus>
				  <ref:ScreeningID>1-S3S8</ref:ScreeningID>
				  <ref:ScreeningDate>2015-04-16T11:08:40</ref:ScreeningDate>
				  <ref:RelatedReferralID />
				  <ref:CreatedDate>2015-04-16T11:51:59</ref:CreatedDate>
				  <ref:DueDate>2015-04-18T11:51:59</ref:DueDate>
				  <ref:RowVersion>2</ref:RowVersion>
				  <per:ClientBasic xmlns:per='http://socialservices.gov.au/ebo/Person'>
					<per:FirstName>Tori</per:FirstName>
					<per:LastName>Lightswitch</per:LastName>
					<per:ID>1-R753</per:ID>
					<per:MiddleName />
					<per:NickName>G</per:NickName>
					<per:Gender>Male</per:Gender>
					<per:BirthDate>1940-02-04</per:BirthDate>
				  </per:ClientBasic>
				  <ref:AssessmentType>Comprehensive Assessment</ref:AssessmentType>
				</ref:AgedCareAssessmentReferral>
				<com:PagingControlOutput xmlns:com='http://socialservices.gov.au/Common'>
				  <com:TotalRecords>1</com:TotalRecords>
				</com:PagingControlOutput>
			  </ass:ListReferralsOutput>
			</ass:ListReferralsResponse>
		  </soap:Body>
		</soap:Envelope>";

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);

XmlNamespaceManager xManager = new XmlNamespaceManager(xDoc.NameTable);
xManager.AddNamespace("ref", "http://socialservices.gov.au/ebo/Referral");

XmlNode xNodeEpisodeID = xDoc.SelectSingleNode("//ref:AgedCareAssessmentReferral", xManager).SelectSingleNode("//ref:EpisodeID", xManager);

string refID = string.Empty;

if (xNodeEpisodeID.InnerText.Trim() == "1-S3S2")
	refID = xDoc.SelectSingleNode("//ref:AgedCareAssessmentReferral", xManager).SelectSingleNode("//ref:ID", xManager).InnerText;

Open in new window


The value of "refID" will be 1-S4H0.


Hope this helps.

Regards,
Ronak
0
 
victoriaharryAuthor Commented:
Thanks, That worked well for the first AgedCareAssessmentReferral node but I should have mentioned this node can appear several times and I need to return the ID for the node that has it. If the episode is in the second or later node it doesn't work. What modifications would I need to check across all the nodes such as the xml below. Thanks

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  <soap:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ass:ListReferralsResponse xmlns:ass="http://socialservices.gov.au/ebm/AssessmentReferral">
      <ass:ListReferralsOutput>
        <com:TransactionStatus xmlns:com="http://socialservices.gov.au/Common">
          <com:TransactionStatusCode>Success</com:TransactionStatusCode>
        </com:TransactionStatus>
        <ref:AgedCareAssessmentReferral xmlns:ref="http://socialservices.gov.au/ebo/Referral">
          <ref:ID>1-S5L1</ref:ID>
          <ref:OutletID>1-KA5J</ref:OutletID>
          <ref:Status>Issued</ref:Status>
          <ref:StatusDescription />
          <ref:StatusReason />
          <ref:Comments />
          <ref:Priority>High</ref:Priority>
          <ref:Source>Aged Care Gateway</ref:Source>
          <ref:EpisodeID>1-R644</ref:EpisodeID>
          <ref:EpisodeStatus>Active</ref:EpisodeStatus>
          <ref:ScreeningID>1-R64A</ref:ScreeningID>
          <ref:ScreeningDate>2015-04-15T17:29:43</ref:ScreeningDate>
          <ref:RelatedReferralID />
          <ref:CreatedDate>2015-04-16T16:15:41</ref:CreatedDate>
          <ref:DueDate>2015-04-18T16:15:41</ref:DueDate>
          <ref:RowVersion>2</ref:RowVersion>
          <per:ClientBasic xmlns:per="http://socialservices.gov.au/ebo/Person">
            <per:FirstName>Jagan</per:FirstName>
            <per:LastName>pages</per:LastName>
            <per:ID>1-RHHV</per:ID>
            <per:MiddleName />
            <per:NickName>G</per:NickName>
            <per:Gender>Male</per:Gender>
            <per:BirthDate>1940-02-04</per:BirthDate>
          </per:ClientBasic>
          <ref:AssessmentType>Comprehensive Assessment</ref:AssessmentType>
        </ref:AgedCareAssessmentReferral>
        <ref:AgedCareAssessmentReferral xmlns:ref="http://socialservices.gov.au/ebo/Referral">
          <ref:ID>1-S4H0</ref:ID>
          <ref:OutletID>1-KA5J</ref:OutletID>
          <ref:Status>Issued</ref:Status>
          <ref:StatusDescription />
          <ref:StatusReason />
          <ref:Comments />
          <ref:Priority>High</ref:Priority>
          <ref:Source>Aged Care Gateway</ref:Source>
          <ref:EpisodeID>1-S3S2</ref:EpisodeID>
          <ref:EpisodeStatus>Active</ref:EpisodeStatus>
          <ref:ScreeningID>1-S3S8</ref:ScreeningID>
          <ref:ScreeningDate>2015-04-16T11:08:40</ref:ScreeningDate>
          <ref:RelatedReferralID />
          <ref:CreatedDate>2015-04-16T11:51:59</ref:CreatedDate>
          <ref:DueDate>2015-04-18T11:51:59</ref:DueDate>
          <ref:RowVersion>2</ref:RowVersion>
          <per:ClientBasic xmlns:per="http://socialservices.gov.au/ebo/Person">
            <per:FirstName>Tori</per:FirstName>
            <per:LastName>Lightswitch</per:LastName>
            <per:ID>1-R753</per:ID>
            <per:MiddleName />
            <per:NickName>G</per:NickName>
            <per:Gender>Male</per:Gender>
            <per:BirthDate>1940-02-04</per:BirthDate>
          </per:ClientBasic>
          <ref:AssessmentType>Comprehensive Assessment</ref:AssessmentType>
        </ref:AgedCareAssessmentReferral>
        <com:PagingControlOutput xmlns:com="http://socialservices.gov.au/Common">
          <com:TotalRecords>2</com:TotalRecords>
       </com:PagingControlOutput>
      </ass:ListReferralsOutput>
    </ass:ListReferralsResponse>
  </soap:Body>
</soap:Envelope>

Open in new window

0
 
Ronak PatelCommented:
Okay, so instead of selecting a single node, you have to select all nodes and then iterate through the XmlNodeList and fetch the refID of each node.

Please refer below code:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);

XmlNamespaceManager xManager = new XmlNamespaceManager(xDoc.NameTable);
xManager.AddNamespace("ref", "http://socialservices.gov.au/ebo/Referral");

XmlNodeList xNodes = xDoc.SelectNodes("//ref:AgedCareAssessmentReferral", xManager);

foreach (XmlNode xNode in xNodes)
{
	XmlNode xNodeEpisodeID = xNode.SelectSingleNode("//ref:EpisodeID", xManager);
	string refID = string.Empty;

	if (xNodeEpisodeID.InnerText.Trim() == "1-S3S2")
		refID = xNode.SelectSingleNode("//ref:ID", xManager).InnerText;
}

Open in new window



Hope this helps.

Regards,
Ronak
0
 
victoriaharryAuthor Commented:
Thanks,

I'm finding with the code that the episodeID from the first node is shown twice when I messagebox it so the episodeid from the 2nd node is never found
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.