如何使用c#获取元素值

时间:2012-09-10 11:33:13

标签: c# xml xml-parsing

XML文件:

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

我必须检查每个节点上的FPData值。我使用上面的代码但没有得到..

4 个答案:

答案 0 :(得分:3)

在XPath中,提供节点的完整路径。

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

正在发生的事情是文档根目录下没有直接 FPData节点。

答案 1 :(得分:2)

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

它将返回FingerPrintUserDetails数组,然后在其中找到FPData

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

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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