How Can I Parse XML Stream specific to XPATH and display directly

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="Google.GData.Client" %>
<%@ Import Namespace="Google.GData.Health" %>
<%@ Import Namespace="Google.GData.Extensions" %>
<br/>

<script runat="server
<br/>

void PrintProfile() {
<br/>

GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("weaver", "exampleCo-exampleApp-1");
authFactory.Token = (String) Session["token"];
<br/>

HealthService service = new HealthService(authFactory.ApplicationName);
service.RequestFactory = authFactory;

HealthQuery profileQuery = new HealthQuery("https://www.google.com/h9/feeds/profile/default");
profileQuery.Digest = true;
<br/>

try
{
HealthFeed feed = service.Query(profileQuery);

foreach (HealthEntry entry in feed.Entries )
{
<span style="white-space:pre XmlExtension ccrExt = (XmlExtension) entry.FindExtension("ContinuityOfCareRecord", "urn:astm-org:CCR");
<br/>

XmlDocument ccrXMLDoc = new XmlDocument();
ccrXMLDoc.ImportNode(ccrExt, true);
XmlNamespaceManager ccrNameManager = new XmlNamespaceManager(ccrXMLDoc.NameTable);
ccrNameManager.AddNamespace("ccr", "urn:astm-org:CCR");

<span style="white-space:pre //use xpath to extract specific CCR elements
<span style="white-space:pre XmlNodeList meds = ccrExt.Node.SelectNodes("//ccr:Body/ccr:Medications/ccr:Medication/ccr:Product/ccr:ProductName/ccr:Text", ccrNameManager);
<span style="white-space:pre XmlNodeList conditions = ccrExt.Node.SelectNodes("//ccr:Body/ccr:Problems/ccr:Problem/ccr:Description/ccr:Text", ccrNameManager);
<span style="white-space:pre XmlNodeList allergies = ccrExt.Node.SelectNodes("//ccr:Body/ccr:Alerts/ccr:Alert/ccr:Description/ccr:Text", ccrNameManager);
<span style="white-space:pre XmlNodeList procedures = ccrExt.Node.SelectNodes("//ccr:Body/ccr:Procedures/ccr:Procedure/ccr:Description/ccr:Text", ccrNameManager);
<span style="white-space:pre XmlNodeList immunizations = ccrExt.Node.SelectNodes("//ccr:Body/ccr:Immunizations/ccr:Immunization/ccr:Product/ccr:ProductName/ccr:Text", ccrNameManager);
//XmlNodeList results = ccrExt.Node.SelectNodes("/Body/Results/Result/Test", ccrNameManager);


PrintElement(meds, "Your Medications");
<span style="white-space:pre PrintElement(conditions, "Your Conditions");
<span style="white-space:pre PrintElement(allergies, "Your Allergies");
<span style="white-space:pre PrintElement(procedures, "Your Procedures");
<span style="white-space:pre PrintElement(immunizations, "Your Immunizations");

XmlNode ccr = ccrExt;
<span style="white-space:pre if (ccr != null)
{
Response.Write("<div style="clear:both; <h3>Entire profile</h3><pre>");
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
ccr.WriteTo(xw);

Response.Write(HttpUtility.HtmlEncode(sw.ToString()));
Response.Write("[/code]");
}
}
}
catch (GDataRequestException gdre)
{
HttpWebResponse response = (HttpWebResponse)gdre.Response;

//bad auth token, clear session and refresh the page
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Session.Clear();
Response.Redirect(Request.Url.AbsolutePath, true);
}
else
{
Response.Write("Error processing request: " + gdre.ToString());
}
}
}

void PrintElement(XmlNodeList nodeList, String title) {
<span style="white-space:pre Response.Write("<span class="right <b>" + title + "</b><ol>");
<span style="white-space:pre foreach (XmlNode element in nodeList)
<span style="white-space:pre {
<span style="white-space:pre Response.Write("" + HttpUtility.HtmlEncode(element.InnerText) + "");
<span style="white-space:pre }
<span style="white-space:pre Response.Write("</ol>");
}
</script>
<br/>

<br/>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server
<title>Google Health API .NET - Profile Read Sample</title>
<style type="text/css
ul,ol {
<span style="white-space:pre margin:0;
<span style="white-space:pre margin-top:10px;
<span style="white-space:pre padding:0;
<span style="white-space:pre list-style:none;
}
pre {
border:1px solid #ccc;
<span style="white-space:pre padding:15px;
}
.right {
<span style="white-space:pre float:left;
<span style="white-space:pre width:200px;
}
</style>
</head>
<body>
<form id="form1" runat="server
<h1>Google Health API - .NET Profile Read Sample</h1>

<%
GotoAuthSubLink.Visible = false;

if (Session["token"] != null)
{
PrintProfile();
}
else if (Request.QueryString["token"] != null)
{
String token = Request.QueryString["token"];
Session["token"] = AuthSubUtil.exchangeForSessionToken(token, null).ToString();
Response.Redirect(Request.Url.AbsolutePath, true);
}
else //no auth data, print link
{
GotoAuthSubLink.Text = "Login to your Google Account";
GotoAuthSubLink.Visible = true;
String authSubLink = AuthSubUtil.getRequestUrl("http", "www.google.com",
"/h9/authsub", Request.Url.ToString(), "https://www.google.com/h9/feeds/", false, true);
authSubLink += "&permission=1";
GotoAuthSubLink.NavigateUrl = authSubLink;
}

%>
<asp:HyperLink ID="GotoAuthSubLink" runat="server"/>

</form>
</body>
</html>

View the full article
 
Back
Top