Say i have these XML tag:

<?xml version="1.0" encoding="utf-8" ?>
<AutoMobile>
  <KeyPhrases list="BMW">
    <Question value="Is this type I or type II?">
      <Option value="Type I" id="1">
        <Question value="Is this controlled or uncontrolled?">
          <Option value="Controlled" id="1">
            <Diagnosis_List>
              <Diagnosis icd9=""> type I - controlled</Diagnosis>
            </Diagnosis_List>
          </Option>
          <Option value="Uncontrolled" id="2">
            <Diagnosis_List>
              <Diagnosis icd9="">type I - uncontrolled</Diagnosis>
            </Diagnosis_List>
          </Option>
        </Question>
      </Option>
      <Option value="Type II" id="2">
        <Question value="Is this controlled or uncontrolled?">
          <Option value="Controlled" id="1">
            <Diagnosis_List>
              <Diagnosis icd9="">diabetes type II - controlled</Diagnosis>
            </Diagnosis_List>
          </Option>
          <Option value="Uncontrolled" id="2">
            <Diagnosis_List>
              <Diagnosis icd9="">type II - uncontrolled</Diagnosis>
            </Diagnosis_List>
          </Option>
        </Question>
      </Option>
    </Question>
</AutoMobile>

And I am using this my code to extract the "Question Value" (highlighted in green in the xml above)

Public Sub New(ByRef data As Xml.XmlNodeList)
        InitializeComponent()

        Me.xData = data
        If 1 = 1 Then

        End If

        Me.txtQuestion.Text = xData.Item(0).Attributes.GetNamedItem("value").Value.ToString

    End Sub

How can I extract the "Option Value" highlighted in red in the xml?

Thanks in advance for any help

Recommended Answers

All 3 Replies

You have multiple elements called Question and Diagnosis, some children of the other, which isn't good XML design.
I prefer using Xpath for XML but you would need to fix your naming clashes before it would work properly. In Xpath your query would look like this:

Dim node As XmlNodeList
node = xdata.SelectSingleNode("/Question[0]/Option")
string result = node.GetAttribute("value")

I think that will work, I did that from memory.

Thanks hericles for your respond. when I try your suggestion I get this error: "selectsinglenode is not a member of system.xml.xmlnodelist"

about the XML design, there is nothing I can really do about it because that's the format it will stay. that's why I am trying to extract the values that I want using xpath

Sorry, I knew doing it from memory would be rusty. Change

Dim node As XmlNodeList

to

Dim node As XmlNode
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.