• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
人生之路,职业之路
读书使人充实,交谈使人机敏,写记使人精确;
“动因+兴趣”——决心-持之以恒-见效
博客园    首页    新随笔    联系   管理    订阅  订阅
XML文件标签名一致,而属性值不同,如何遍历取值?
- <EssentialFunctions>
     <Qualification description="We Offer" source="AdDesc1">LOCAL, NON-EXPATRIATE PAY + BENEFITS + ANNUAL LEAVE EMPLOYEE STATUS:REGULAR VISA:SPONSORED*, OVERSEAS CANDIDATES ELIGIBLE TO APPLY AIRFARE:NOT SPONSORED HOUSING:NOT SPONSORED MEDICAL INSURANCE:INCLUDED </Qualification>
     <Qualification description="Roles and Responsibilities" source="AdDesc2">1、 Service business delivery process design and realization 2、 Business process optimization 3、 Document management 4、 External communications</Qualification>
     <Qualification description="Requirements" source="AdDesc3">Minimum requirements: 1. Bachelor’s degree or above with a related major (IT Management, Business Administration , technology etc.) </Qualification>
     <Qualification description="Additional Information" source="AdDesc4">The ultimate interpretation rights of all above belongs to Legend Group HR headquarters. In case there are inconsistencies between the English and the Chinese version, the Chinese language version applies.</Qualification>
     <Qualification source="Language">EN</Qualification>
     <Qualification source="LOV5305" category="Function Area">客户服务</Qualification>
     <Qualification source="LOV5510" category="Location">北京市</Qualification>
</EssentialFunctions>

在C#中如何循环读取属性及内容?
通过node.Attributes["属性名"].Value获取属性值
当作普通节点对待,遍历节点来获取所有节点值.

StringBuilder sb = new StringBuilder();

XmlNode ndQualification = el.SelectSingleNode("JobPositionInformation/JobPositionDescription/EssentialFunctions");
string _attr_source = "";
string _attr_desc = "";
string _attr_category = "";
foreach (XmlNode node in ndQualification.ChildNodes)
{  
              
      if (!XmlUtility.IsEmpty(node))
      {
             if(node.Attributes["source"] != null)
                   _attr_source = (String.IsNullOrEmpty(node.Attributes["source"].Value))?"":node.Attributes["source"].Value;
             if(node.Attributes["description"] != null)
                  _attr_desc = (String.IsNullOrEmpty(node.Attributes["description"].Value))?"":node.Attributes["description"].Value;
             if(node.Attributes["category"] != null)
                   _attr_category = (String.IsNullOrEmpty(node.Attributes["category"].Value))?"":node.Attributes["category"].Value;
             if(_attr_source.Length > 0)
                  sb.Append(string.Format(" <b>source:</b>{0};", _attr_source));
             if(_attr_desc.Length > 0)
                   sb.Append(string.Format(" <b>description:</b>{0};", _attr_desc));
             if(_attr_category.Length > 0)
                   sb.Append(string.Format(" <b>category:</b>{0};", _attr_category));
             sb.Append("<br>");
             sb.Append(node.InnerText.Trim()); //节点值
             sb.Append("<br><br>");

             _attr_source = "";
             _attr_desc = "";
             _attr_category = "";
        }
}
         
 return sb.ToString();

C#遍历XML文件所有节点源代码
posted on 2008-04-21 16:04  FreeBird  阅读(1352)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3