How to get element value using c#

11,891

Solution 1

In your XPath, provide the full path to the node.

XmlNodeList nodes = root.SelectNodes("/content/FingerPrintUserDetails/FPData");

What is happening is that there is no direct FPData node under the document root.

Solution 2

XmlNodeList nodes = root.SelectNodes("content/FingerPrintUserDetails");

it will return array of FingerPrintUserDetails, then find FPData in them

XmlNodeList res = nodes[index].SelectNodes("FPData");

Solution 3

Using LINQ to XML:

 XDocument doc = XDocument.Load("XmlFilePath");
 var selectors = from elements in doc.Elements("content").Elements("FingerPrintUserDetails")
                 select elements;

foreach (var element in selectors)
 {
    MessageBox.Show(element.Element("FPData").Value);
 }

Solution 4

XmlDocument doc = new XmlDocument();
doc.Load("E://BioEnable_Project//fp_project_using_xml//fp_project_using_xml//Capture_Data.xml");
XmlNodeList lst =  doc.GetElementsByTagName("FingerPrintUserDetails");

foreach (XmlElement elem in lst)
{
    XmlNode pfData = doc.GetElementsByTagName("FPData")[0];
    MessageBox.Show(pfData.Value);
}
Share:
11,891
lax
Author by

lax

Updated on June 04, 2022

Comments

  • lax
    lax almost 2 years

    XML file:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>    
    <content>
          <FingerPrintUserDetails>
            <UserID>e57645</UserID>
            <UserName>Jill</UserName>
            <FPData>AQAAABQAAAD0AAAAAQASAAEAWgAAAAAA8AAAAHfrWpB6BHZBL10voxpdxu2Km5XVNh*oUNC80Rvpql3RhlqOiBPeet6iRPwb/BdN1fCF4Y/WHbQ40*mqoUKqilgN7bUqNuXP7M299HUWtoAGEO3nDKXXAnHd7dgytZbmHVv*mRBPJDSRw9VY/R1yOIu2cCDlLM*F8Q1lvTfMFDdfwNZynI0e2ZauCF58f0UX56XLFBallaAauxP5mvvhUmcmc6ITg7RhH9wc4181kgPjCuZg38pQepE5U07XIa3hQP8fwxPzdprifXECgB1Z3pTXWQP0q4ZD0Inlbq6Gszo1ucPrhQA0jYQRXtJUVuyBeg</FPData>
            <Address>Pune</Address>
            <ContactNo>848488484884</ContactNo>
          </FingerPrintUserDetails>
    
    
          <FingerPrintUserDetails>
            <UserID>444</UserID>
            <UserName>John</UserName>
            <FPData>AQAAABQAAADkAAAAAQASAAEAZAAAAAAA4AAAAPLnmQ8FymAAHWYutR5pdtYvfDVmjsqLeli8tOSTFAtw6AkfA0r8XwrMzp9jFZJI7DlBk4G94BMq55gPEG7uBLZUNYrvhv0jDlDFMOjWGJ9RoWekFveTC*oZ7Tq/xmxuvY6FzLHVo*xzdKQI73Y0f9/eeMC0OgqnbQ3I0IP6cTkkKnTUZJOXKr7IFPHkjJAvCDmU7ec4vG50JHdBJIObmbzVcO0huTUQyE7CR1qYkUjmNFKgVKWPLRupEk4l/Ek0BuAba*9JlhBVUHzZuKbDQLc9lTFwevAgDuuAwxfZaLS*</FPData>
            <Address>nagpur</Address>
            <ContactNo>464645763</ContactNo>
          </FingerPrintUserDetails>
    
    
          <FingerPrintUserDetails>
            <UserID>5555</UserID>
            <UserName>Jack</UserName>
            <FPData>AQAAABQAAAAEAQAAAQASAAEAZAAAAAAA9AAAAPz5mQO3uTeXLfU5Mb74XbCX5rERGZFPQMVG1vPpX87306O5oURlYiIe5dasJ2S8NlBZu2UU3zaUpNnB7viYDB6*wfFlgtopn/WdbXW0Yhik3hj8nDreEmaK12To8qfAJx2ooq43i0wBIL*0Jkba*QpHIprSajrhnCg1PjOLMP37sEauJUwXJaoDR/PPQYIxTFE5kf8xzGlJmqiGejD*Y8R3ewU9yIrxkdQ0S//LCdacULt2QvS/I3APo/j0FAgSCOU3SBLdDL6UBPD4fLeEzo7uUIW8gUMThzZX*u2iUuNwJdqWC2NsFtWkUWt03sz3xYQpR8pLA4vrsUmldzUMWe8</FPData>
            <Address>beed</Address>
            <ContactNo>5745745747</ContactNo>
          </FingerPrintUserDetails>
    
    
        </content>
    

    C#:

    XmlDocument doc = new XmlDocument();
    doc.Load("E://BioEnable_Project//fp_project_using_xml//fp_project_using_xml//Capture_Data.xml");
    XmlElement root = doc.DocumentElement;
    XmlNodeList nodes = root.SelectNodes("FPData");
    foreach(XmlElement node in nodes)
    {
        MessageBox.Show(node.Value);
    }   
    

    I have to check FPData value on each node..i use above code but not getting..

  • Oded
    Oded over 11 years
    @lax - Forgot to start from the root. Added a / at the path start.
  • lax
    lax over 11 years
    i have to get FPData where USerID="e57645" how will be my code where UserID coming from textbox
  • Oded
    Oded over 11 years
    @lax - That really is a different question from the above. But this can be done with xpath as well: "/content/FingerPrintUserDetails[UserID = 'e57645']/FPData". How you construct the XPath I leave to you...
  • Oded
    Oded over 11 years
    @lax - Don't know what that means. I tested the above XPath with the XML file you posted and got a single FPData node.
  • lax
    lax over 11 years
    i used in my code like this XmlNodeList nodes = root.SelectNodes("/content/FingerPrintUserDetails['" + txtUserId.Text.Trim() + "']/FPData");