在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();