0

I'm trying to move to another attribute in an xml element but am not having much luck doing this.

Xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
   <finances> 
       <financial no="0" id="431" rowID="0" ddlValue="Return" ddlNote="Post Dated" ddBranch="" ddType="" /> 
   </finances>

Code :

Dim child = 0
child = childRow.RowIndex
Dim hidden = CType(gvChild.DataKeys(child)("id"), Integer)

For Each node As XmlNode In doc.SelectNodes("//financial/@id")
    If node.Value = hidden.ToString Then
       If Not doc.SelectSingleNode("//financial/@ddlNote").InnerText.Equals("") Then
             LblErr.Text = doc.SelectSingleNode("//financial/@ddlNote").InnerText
       End If

    End If
Next

Basically, when the id equals what's on my form I want it to move to the ddlNote attribute and display the value from it. It triggers the first if statement, when it matches up with the id that's on the form it moves to the inner if statement, but the inner if statement won't trigger. How would I move to that attribute (seeing as I'm starting at the id attribute, how would I move to ddlNote)?

1 Answer 1

1

Your answer is one line above the if statement that doesn't trigger. Your xpath is selecting attributes, so in the first if you're selecting the value of the attribute. Simply repeat it on the second if and select the value instead of the inner text:

If node.Value = hidden.ToString Then
   If Not doc.SelectSingleNode("//financial/@ddlNote").Value.Equals("") Then
         LblErr.Text = doc.SelectSingleNode("//financial/@ddlNote").Value
   End If
End If

...BUT I would change your approach, first because you can use one node for both ifs and second because with your actual code, if the XML has more than one node, you will always get the ddlNote attribute of the first one if it is not blank. So I would do something like this:

For Each node As XmlNode In doc.SelectNodes("//financial")
    If node.Attributes("id").Value = hidden.ToString Then
        If Not node.Attributes("ddlNote").Value.Equals("") Then
            LblErr.Text = node.Attributes("ddlNote").Value
        End If
    End If
Next

You just need to read the node once and then access it's attributes by name. Remember to use the value property, as innerText gets the full text of the node and subnodes.

EDIT: Even shorter:

For Each node As XmlNode In doc.SelectNodes(String.Format("//financial[@id={0}]", hidden.ToString)
    If Not node.Attributes("ddlNote").Value.Equals("") Then
        LblErr.Text = node.Attributes("ddlNote").Value
    End If
Next

You can use XPath to directly select the node that has the attribute value you are searching.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I've found a solution, very similar to the last part you posted here, thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.