2015-10-12 91 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);