• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

I want to capture an XML attribute value in a string variable

<!-- This is what the XML File looks like
This challenge is I don't know the attribute's value (because it's dynamically created) however I will always know the username value.-->

<!--ALSO LET ME KNOW IF I SHOULD JUST USE TEXT MEMBER INSTEAD OF XML MEMBERS-->

<users>
  <users
    <role>
        admin
    </role>
    <username ID="2CF8C226-E8BE-4428-8C00-C6A047CC9024">
       john
    </username>
    <password ID="2CF8C226-E8BE-4428-8C00-C6A047CC9024">>
      password
    </password>
  </user>
</users>

<!-- Psuedo code:
go get the value of the ID attribute where the username is equal to john-->
// An attempt to achieve this:
 
xmlDocPath = Server.MapPath("XMLAuth/userAuthentication.xml")
        doc = New XPathDocument(xmlDocPath)
        nav = doc.CreateNavigator()
// Try to find the username john so we can store it in a variable
// Then get the the value of the id attribute
// Store that value in a variable
// Then go get the password where ID attribute is equal to the attribute we found in username element
xPathIter = nav.[Select]("users/user/username")
 
        If xPathIter.Count > 0 Then
            While xPathIter.MoveNext()
                Response.Write(xPathIter.Current.InnerXml.ToString())
            End While
        End If

Open in new window

0
kvigor
Asked:
kvigor
1 Solution
 
shahjapanCommented:

XmlDocument xml_arch_doc = new XmlDocument();
xml_arch_doc.Load("ilename.xml");
 
XmlNodeList tree_nodes = xml_arch_doc.GetElementsByTagName("username");
foreach(XmlNode node in tree_nodes)
{
    string userID = node.Attributes["ID"].Value
    MessageBox.Show(userID)
}      

Open in new window

0
 
kvigorAuthor Commented:
Thanks it works to target the.  I would've liked to see how to traverse the doc from this code though.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now