如何使用c#从XML文件中获取子属性值?

时间:2022-11-27 23:20:59

I am having xml file with name as employeesInfo and in that the data is like

我有一个名为employeesInfo的xml文件,数据是这样的

<?xml version="1.0" encoding="utf-8"?>
<EmployeeDetails>
  <data>
    <empCode>DIT-GINT-0001</empCode>
    <FirstName>Dinesh</FirstName>
    <LastName>Alla</LastName>
    <Address>Guntur</Address>
  </data>
  <EmployeeDetails>
    <empCode>DIT-GINT-0002</empCode>
    <FirstName>Upendra</FirstName>
    <LastName>Maddi</LastName>
    <Address>guntur</Address>       
  </EmployeeDetails>
  <EmployeeDetails>
    <empCode>DIT-GINT-0003</empCode>
    <FirstName>Chandrkanth</FirstName>
    <LastName>Beth</LastName>
    <Address>guntur</Address>       
  </EmployeeDetails>
</EmployeeDetails>

And My code to get attribute{"empCode"} value is

我的代码获得属性{“empCode”}值。

XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/employeesInfo.xml"));
XElement root = doc.Root;
string s = root.Elements("empCode").Last().ToString();

Requirement: I want to get empcode value of last parent element.

需求:我想获得上一个父元素的empcode值。

Example output of the above file: DIT-GINT-0003

上面文件的输出示例:DIT-GINT-0003

3 个解决方案

#1


1  

This can do the work -

这可以做这项工作

XDocument doc = XDocument.Load(@"D:\Temp\asd.xml");
var s = doc.Descendants("empCode").Last().Value;

To check whether the node exists or not -

检查节点是否存在。

XDocument doc = XDocument.Load(@"D:\Temp\asd.xml");
var empCodes = doc.Descendants("empCode");
string result = string.Empty;

if(empCodes.Count() > 0)
{
     result = empCodes.Last().Value;
}

#2


2  

In your code sample root is <EmployeeDetails>. Get its childnodes and select the last one, then get the <empCode> of this node. I am sorry that I am writing this as an answer, I don't have enough reputation.

在您的代码示例根中,是 。获取它的子节点并选择最后一个节点,然后获取该节点的 。很抱歉,我写这篇文章是为了回答这个问题,我没有足够的声誉。

XElement lastEmployeeDetails = root.LastNode;

#3


2  

To get the last element and specific value in your case

获取案例中的最后一个元素和特定值

XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/employeesInfo.xml"));
XElement root = doc.Root;
string s = root.Element("EmployeeDetails").Element("empCode").LastNode.ToString();

#1


1  

This can do the work -

这可以做这项工作

XDocument doc = XDocument.Load(@"D:\Temp\asd.xml");
var s = doc.Descendants("empCode").Last().Value;

To check whether the node exists or not -

检查节点是否存在。

XDocument doc = XDocument.Load(@"D:\Temp\asd.xml");
var empCodes = doc.Descendants("empCode");
string result = string.Empty;

if(empCodes.Count() > 0)
{
     result = empCodes.Last().Value;
}

#2


2  

In your code sample root is <EmployeeDetails>. Get its childnodes and select the last one, then get the <empCode> of this node. I am sorry that I am writing this as an answer, I don't have enough reputation.

在您的代码示例根中,是 。获取它的子节点并选择最后一个节点,然后获取该节点的 。很抱歉,我写这篇文章是为了回答这个问题,我没有足够的声誉。

XElement lastEmployeeDetails = root.LastNode;

#3


2  

To get the last element and specific value in your case

获取案例中的最后一个元素和特定值

XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/employeesInfo.xml"));
XElement root = doc.Root;
string s = root.Element("EmployeeDetails").Element("empCode").LastNode.ToString();