2016-07-15 133 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; 
    } 
} 
相关问题