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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.