Question about XML XPath
-
Hello, I have a xml document like this: < ? xml version = " 1.0 " ? > < Accounts > < Account ID = " 123456 " > < FirstName > Latheesan < / FirstName > < SecondName > Kanes < / SecondName > < Balance > 0 < / Balance > < OverDraftLimit > 50 < / OverDraftLimit > < FullAddress > My Address Here < / FullAddress > < / Account > < / Accounts > What i am trying to do is select one matching document node based on my XPath expression and then when found, assign each element node's value to set of strings. So far, this is what i was able to do:
private void searchBtn1_Click(object sender, EventArgs e) { try { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); // Compile Standard XPath Expression XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); // Results nav.MoveToFirstChild(); MessageBox.Show(nav.Value); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } }
I printed the XPath Expression result using MessageBox.Show to see what the results would be like. On the message box, i saw the entire document node and it's element nodes and their values. How do you obtain the results after running XPath expression and assign element node's value like this: string AccountID = nav.value; string FirstName = nav.value; string SecondName = nav.value; string Balance = nav.value; string OverDraftLimit = nav.value; string FullAddress = nav.value; -
Hello, I have a xml document like this: < ? xml version = " 1.0 " ? > < Accounts > < Account ID = " 123456 " > < FirstName > Latheesan < / FirstName > < SecondName > Kanes < / SecondName > < Balance > 0 < / Balance > < OverDraftLimit > 50 < / OverDraftLimit > < FullAddress > My Address Here < / FullAddress > < / Account > < / Accounts > What i am trying to do is select one matching document node based on my XPath expression and then when found, assign each element node's value to set of strings. So far, this is what i was able to do:
private void searchBtn1_Click(object sender, EventArgs e) { try { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); // Compile Standard XPath Expression XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); // Results nav.MoveToFirstChild(); MessageBox.Show(nav.Value); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } }
I printed the XPath Expression result using MessageBox.Show to see what the results would be like. On the message box, i saw the entire document node and it's element nodes and their values. How do you obtain the results after running XPath expression and assign element node's value like this: string AccountID = nav.value; string FirstName = nav.value; string SecondName = nav.value; string Balance = nav.value; string OverDraftLimit = nav.value; string FullAddress = nav.value;Nevermind, i worked it out =D
private void searchBtn1_Click(object sender, EventArgs e) { try { string fileName = "Account_Data.xml"; XPathDocument doc = new XPathDocument(fileName); XPathNavigator nav = doc.CreateNavigator(); // Compile Standard XPath Expression XPathExpression expr; expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']"); XPathNodeIterator iterator = nav.Select(expr); iterator = nav.Select(expr); if (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); string accID = (nav2.GetAttribute("ID", "")); nav2.MoveToFirstChild(); string firstName = (nav2.Value); nav2.MoveToNext(); string lastName = (nav2.Value); nav2.MoveToNext(); string currentBalance = (nav2.Value); nav2.MoveToNext(); string overDraftLimit = (nav2.Value); nav2.MoveToNext(); string = fullAddress = (nav2.Value); } } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } }