2

次の XML ドキュメント情報があります。

<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd" xmlns="urn:hl7-org:v3" xmlns:cda="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc">
  <component>
    <structuredBody>
      <component>
        <section>
          <templateId root="2.16.840.1.113883.10.20.22.2.45" />
          <title>Instructions</title>
          <text>
            <paragraph>Putting instructions into the node to read out into the text area.</paragraph>
          </text>
        </section>
      </component>
    </structuredBody>
  </component>
</Document>

この特定の情報を検索し、段落ノードのコンテンツを Web ページのテキスト領域フォーム フィールドに配置するには、XML ドキュメントを VB.NET ページでロードする必要があります。これは問題なく動作していましたが、ドキュメント ルートにいくつかの名前空間情報が追加されたため、動作しなくなりました。これは VB.NET コードです。

m_nodelist = m_xmld.SelectNodes("Document/component/structuredBody/component/section")
For Each m_node In m_nodelist
  If m_node("title").InnerText = "Instructions" Then
    Dim Instructions As String = m_xmld.SelectSingleNode("Document/component/structuredBody/component/section[title='Instructions']/text/paragraph").InnerText
    txtInstructions.Text = Replace(patientInstructions, "<br>", Chr(10) & Chr(13))
    hfInstructionsOld.Value = Replace(patientInstructions, "<br>", Chr(10) & Chr(13))
  End If
Next

ルートノードには名前空間が定義されていなかったため、以前は機能していたと思います。今ではそうです。それを考慮して VB.NET コードを変更する方法がわかりません。

4

1 に答える 1

1

デフォルトの名前空間がルート要素で定義されるようになったため、すべての要素がデフォルトでその名前空間に属します。XPath を使用する場合 (および で行っているようSelectNodesSelectSingleNodes)、名前空間を常に明示的に指定する必要があります。XPath では、名前空間が明示的に指定されていない場合に常に使用されるデフォルトの名前空間を指定する方法はありません。方法は次のとおりです。

Dim nsmgr As New XmlNamespaceManager(m_xmld.NameTable)
nsmgr.AddNamespace("x", "urn:hl7-org:v3")
m_nodelist = m_xmld.SelectNodes("x:Document/x:component/x:structuredBody/x:component/x:section", nsmgr)
For Each m_node In m_nodelist
    If m_node("title").InnerText = "Instructions" Then
        Dim Instructions As String = m_xmld.SelectSingleNode("x:Document/x:component/x:structuredBody/x:component/x:section[x:title='Instructions']/x:text/x:paragraph", nsmgr).InnerText
        txtInstructions.Text = Replace(patientInstructions, "<br>", Chr(10) & Chr(13))
        hfInstructionsOld.Value = Replace(patientInstructions, "<br>", Chr(10) & Chr(13))
    End If
Next

または、ルート要素から名前空間を動的に取得するだけの場合は、次のようにすることができます。

nsmgr.AddNamespace("x", m_xmld.DocumentElement.NamespaceURI)
于 2013-09-16T15:22:59.867 に答える