2015-10-12 92 views
-2

這是我的XML文件如何讀取節點XML與條件

<problem> 
    <sct:fsn>Myocardial infarction (disorder)</sct:fsn> 
    <sct:code>22298006</sct:code> 
    <sct:description>Heart attack</sct:description> 
    <sct:description>Infarction of heart</sct:description> 
    <sct:description>MI - Myocardial infarction</sct:description> 
    <sct:description>Myocardial infarct</sct:description> 
    <sct:description>Cardiac infarction</sct:description> 
    <sct:description>Myocardial infarction</sct:description> 
</problem> 

如何選擇代碼和FSN?如果我有描述。 請幫忙謝謝

+0

'xml'文件在哪裏? –

+1

@ArghyaC:它在那裏。但不在代碼塊中因此看不到。我已經格式化了這個帖子。希望能儘快看到 –

+1

你的描述是所有''節點的連接? –

回答

0

你錯過了一個重要部分:命名空間聲明。

在您的原始XML文件中,您可能有一個或多個屬性,如xmlns:xxx。這些屬性具有特殊含義,因爲允許在同一文件中使用兩個不同的XML 詞彙表

要查找與命名空間的這些元素(注意xxx:一部分;你的情況,這是sct:),你需要使用XmlNamespaceManager類,如下面的樣本。

被警告:屬性值(本例中的「示例」)必須與逐字逐句使用的AddNamespace方法相同。

var xml = new XmlDocument(); 
    xml.LoadXml(@" 
     <problem xmlns:sct='example'> 
      <sct:fsn>Myocardial infarction (disorder)</sct:fsn> 
      <sct:code>22298006</sct:code> 
      <sct:description>Heart attack</sct:description> 
      <sct:description>Infarction of heart</sct:description> 
      <sct:description>MI - Myocardial infarction</sct:description> 
      <sct:description>Myocardial infarct</sct:description> 
      <sct:description>Cardiac infarction</sct:description> 
      <sct:description>Myocardial infarction</sct:description> 
     </problem>"); 

var xmlns = new XmlNamespaceManager(xml.NameTable); 
    xmlns.AddNamespace("sct", "example"); 
    // same as  xmlns:sct= 'example' 

// This is important: ---------------------------------V 
Console.WriteLine(xml.SelectSingleNode("//sct:code", xmlns).InnerText);