Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Question about XML XPath

Question about XML XPath

Scheduled Pinned Locked Moved C#
xmlquestionhelpannouncement
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Latheesan
    wrote on last edited by
    #1

    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;

    L 1 Reply Last reply
    0
    • L Latheesan

      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;

      L Offline
      L Offline
      Latheesan
      wrote on last edited by
      #2

      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); } }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups