2016-07-15 69 views
-2
static void Main(string[] args) 
     { 

      WebClient _httpReq = new WebClient(); // to talk to the web only for get method 
      string response = _httpReq.DownloadString("https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/Patient?family=Argonaut&given=Jason"); 
Console.WriteLine(response);\\prints the xml string fetched from FHIR provider EPIC 
      XmlDocument xml = new XmlDocument(); 
      xml.LoadXml(response); // suppose that myXmlString contains "<Names>...</Names>" 

      XmlNodeList xnList = xml.SelectNodes("/entry/resource/patient/name"); 
// here code is trying to extract the name attribute 
      foreach (XmlNode xn in xnList) 
      { 
       string firstName = xn["family value"].InnerText; 
       string lastName = xn["given value"].InnerText; 
       Console.WriteLine("Name: {0} {1}", firstName, lastName); 
       //print the first name and last name of the patient 
      } 
      Console.ReadLine(); 

     } 
+2

什麼是你的問題? –

+3

我強烈建議使用LINQ to XML而不是XmlDocument - 它是一個更好的API。 –

回答

0

評論XPath。一旦你開始理解它,你會發現它比遍歷列表更有效且更容易編碼。它也可以讓你直接獲得你想要的節點/屬性。

然後代碼將是類似的東西,以

string attrVal = doc.SelectSingleNode("/entry/resource/patient/@name").Value; 

或者你可以在XMLDOCUMENT加載XML,你可以取件和出該元素的您可以閱讀特定atairbute,

XmlDocument doc = new XmlDocument(); 
doc.LoadXml("<reply success=\"true\">More nodes go here</reply>"); 

XmlElement root = doc.DocumentElement; 

string s = root.Attributes["success"].Value; 
1

我像這樣做:

XmlDocument MyDocument = new XmlDocument(); 
MyDocument.Load("..."); 

XmlNode MyNode = MyDocument.SelectSingleNode("/Node_Name"); 

foreach (XmlAttribute MyAttribute in MyNode.Attributes) 
{ 
    if (MyAttribute.Name == "Attribute_Name") 
    { 
     object Value = MyAttribute.Value; 
     break; 
    } 
} 
相關問題