Linq处理Xml文档的封装

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml.Linq;
using System.Linq;

namespace MyQuery.Utils
{
/// <summary>
/// Linq处理Xml文档的封装
/// 作者 jiashiyi
/// </summary>
public static class LinkXmlHelper
{
/// <summary>
/// 获得子节点实体
/// </summary>
/// <param name="element">节点</param>
/// <param name="nodeName">子节点的定义</param>
/// <param name="name">子节点的属性名称</param>
/// <param name="value">子节点的属性值</param>
/// <returns>子节点属性值为指定值的,若null没找到,否则返回第一个匹配的</returns>
public static XElement GetElement(XElement element, XName nodeName, XName name, string value)
{
if (element == null || element.Element(nodeName) == null)
{
return null;
}
else
{
var query =
from
i in element.Elements(nodeName)
where
i.Attribute(name) != null && i.Attribute(name).Value.Equals(value, StringComparison.CurrentCultureIgnoreCase)
select i;
if (query == null || query.Count() < 1)
{
return null;
}
else
{
foreach (XElement result in query)
{
return result;
}
return null;
}
}
}
/// <summary>
/// 获得节点name数组指定的最后一个节点
/// </summary>
/// <param name="element">节点</param>
/// <param name="names">节点name数组</param>
/// <returns>name数组指定的最后一个节点,若null没找到,否则返回第一个匹配的</returns>
public static XElement GetElement(XElement element, XName[] names)
{
if (element == null || names == null || names.Length < 1)
{
return null;
}
else
{
XName name = names[0];
if (name == null || element.Element(name) == null)
{
return null;
}
else
{
element = element.Element(name);
for (int i = 1; i < names.Length; i++)
{
name = names[i];
if (element == null || name == null)
{
return null;
}
else
{
element = element.Element(name);
}
}
}
return element;
}
}
/// <summary>
/// 获得element下node指定的子节点实体
/// </summary>
/// <param name="element">节点</param>
/// <param name="node">element下子节点名称</param>
/// <returns>element下node指定的子节点实体,若null没找到,否则返回第一个匹配的</returns>
public static XElement GetElement(XElement element, XName node)
{
return GetElement(element, new XName[] { node });
}
/// <summary>
/// 获得root下node指定的节点实体
/// </summary>
/// <param name="doc">XDocument实体</param>
/// <param name="node">root下节点名称</param>
/// <returns>root下node指定的节点实体,若null没找到,否则返回第一个匹配的</returns>
public static XElement GetElement(XDocument doc, XName node)
{
return GetElement(doc.Root, new XName[] { node });
}
/// <summary>
/// 获得节点name数组指定的最后一个节点的集合
/// </summary>
/// <param name="element">节点</param>
/// <param name="names">节点name数组</param>
/// <returns>name数组指定的最后一个节点的集合,若null没找到,否则返回集合</returns>
public static IEnumerable<XElement> GetElements(XElement element, XName[] names)
{
if (element == null || names == null || names.Length < 1)
{
return null;
}
else
{
XName name = names[0];
if (name == null || element.Element(name) == null)
{
return null;
}
else if (names.Length == 1)
{
return element.Elements(name);
}
else
{
element = element.Element(name);
for (int i = 1; i < names.Length; i++)
{
name = names[i];
if (element == null || name == null || element.Element(name) == null)
{
return null;
}
else if (i == names.Length - 1)
{
return element.Elements(name);
}
else
{
element = element.Element(name);
}
}
}
return null;
}
}
/// <summary>
/// 获得节点name数组指定的最后一个节点的集合
/// </summary>
/// <param name="element">节点</param>
/// <param name="names">节点name数组</param>
/// <param name="nodeName">子节点的定义</param>
/// <param name="name">子节点的属性名称</param>
/// <param name="value">子节点的属性值</param>
/// <returns>name数组指定的最后一个节点的集合的nodeName子节点属性值为指定值的,若null没找到,否则返回集合</returns>
public static IEnumerable<XElement> GetElements(XElement element, XName[] names, XName nodeName, XName name, string value)
{
if (names != null)
{
element = GetElement(element, names);
}
if (element == null || element.Element(nodeName) == null)
{
return null;
}
else
{
return
from
i in element.Elements(nodeName)
where
i.Attribute(name) != null && i.Attribute(name).Value.Equals(value, StringComparison.CurrentCultureIgnoreCase)
select i;
}
}
/// <summary>
/// 获得指定属性值
/// </summary>
/// <param name="element">节点实体</param>
/// <param name="name">属性名</param>
/// <returns>属性值(原始值),若""则未设置此属性</returns>
public static string GetAttributeOriginal(XElement element, XName name)
{
if (element == null || name == null)
{
return "";
}
else
{
XAttribute result = element.Attribute(name);
if (result == null)
{
return "";
}
else
{
return result.Value;
}
}
}
/// <summary>
/// 获得指定属性值(已转化为小写)
/// </summary>
/// <param name="element">节点实体</param>
/// <param name="name">属性名</param>
/// <returns>属性值(已转化为小写),若""则未设置此属性</returns>
public static string GetAttribute(XElement element, XName name)
{
return GetAttributeOriginal(element, name).Trim().ToLower();
}
/// <summary>
/// 获得指定属性值 若""则返回默认值def (已转化为小写)
/// </summary>
/// <param name="element">节点实体</param>
/// <param name="name">属性名</param>
/// <param name="def"></param>
/// <returns>属性值(已转化为小写),若""则返回默认值def</returns>
public static string GetAttribute(XElement element, XName name, string def)
{
string result = GetAttribute(element, name);
if (result == "")
{
return def;
}
else
{
return result;
}
}
/// <summary>
/// 获得属性的数值
/// </summary>
/// <param name="element">节点</param>
/// <param name="name">属性</param>
/// <param name="def">默认值</param>
/// <returns></returns>
public static int GetIntValue(XElement element, string name, int def)
{
int result = def;
string resultStr = GetAttribute(element, name);
if (resultStr == "")
{
return def;
}
else
{
return DataHelper.GetIntValue(resultStr, def);
}
}
/// <summary>
/// 获得节点的值
/// </summary>
/// <param name="element">节点实体</param>
/// <returns>值,若""则无值</returns>
public static string GetValue(XElement element)
{
if (element == null || element.HasElements)
{
return "";
}
else
{
string result = element.Value;
if (result == null)
{
return "";
}
else
{
return result.Trim().Replace("\n", "").Replace("\r", "");
}
}
}
/// <summary>
/// 获得节点的值
/// </summary>
/// <param name="element"></param>
/// <param name="xNames"></param>
/// <returns></returns>
public static string GetValue(XElement element, XName[] xNames)
{
return GetValue(GetElement(element, xNames));
}
/// <summary>
/// 给指定的属性赋值
/// </summary>
/// <param name="element">节点实体 空不能赋值</param>
/// <param name="name">属性值 属性值为空不能赋值</param>
/// <param name="value">值对象 空或ToString()==""则不赋值 去除特殊字符</param>
public static void SetAttribute(XElement element, XName name, object value)
{
if (element != null && name != null && value != null && !Convert.IsDBNull(value))
{
string v = "";
if (value is DateTime)
{
v = Convert.ToDateTime(value).ToString(Constants.DATE_FORMART);
}
else
{
v = GetAttributeValue(value.ToString().Trim());
}
if (v != "")
{
element.SetAttributeValue(name, v);
}
}
}
/// <summary>
/// 给指定的属性赋值
/// </summary>
/// <param name="element">节点</param>
/// <param name="names">节点name数组 给最后一个节点的属性赋值 为空不能赋值</param>
/// <param name="attName">属性值 属性值为空不能赋值</param>
/// <param name="value">值对象 空或ToString()==""则不赋值 去除特殊字符</param>
public static void SetAttribute(XElement element, XName[] names, XName attName, string value)
{
SetAttribute(GetElement(element, names), attName, value);
}
/// <summary>
/// 给指定的属性赋值
/// </summary>
/// <param name="element">节点</param>
/// <param name="names">节点name数组 给最后一个节点的属性赋值 为空不能赋值</param>
/// <param name="nvs">属性及值集合</param>
public static void SetAttribute(XElement element, XName[] names, NameValueCollection nvs)
{
element = GetElement(element, names);
if (element != null && nvs != null)
{
for (int i = 0; i < nvs.Count; i++)
{
XName name;
if (element.Name.Namespace != null)
{
name = element.Name.Namespace + nvs.GetKey(i);
}
else
{
name = nvs.GetKey(i);
}
SetAttribute(element, name, nvs[i]);
}
}
}
/// <summary>
/// 去除属性值中不符合xml要求的字符
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static string GetAttributeValue(string value)
{
if (value == "")
{
return "";
}
else
{
return value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt").Replace("\"", "&quot;").Replace("%", "");
}
}
/// <summary>
/// 给指定的节点赋值(如果节点不存在则创建)
/// </summary>
/// <param name="element">节点</param>
/// <param name="nvs">节点及值集合</param>
public static void SetValue(XElement element, NameValueCollection nvs)
{
if (element != null && nvs != null)
{
for (int i = 0; i < nvs.Count; i++)
{
XName name;
if (element.Name.Namespace != null)
{
name = element.Name.Namespace + nvs.GetKey(i);
}
else
{
name = nvs.GetKey(i);
}
string value = nvs[i];
if (!String.IsNullOrEmpty(value))
{
XElement sub = element.Element(name);
if (sub != null)
{
sub.SetValue(value);
}
else
{
sub = new XElement(name);
sub.SetValue(value);
element.Add(sub);
}
}
}
}
}

/// <summary>
/// 获得节点title原始值
/// </summary>
/// <param name="element">节实体</param>
/// <returns>获得节点title原始值,返回""则未配置</returns>
public static string GetTitle(XElement element)
{
return GetAttributeOriginal(element, "title").Replace("&lt;", "<").Replace("&gt;", ">");
}
/// <summary>
/// 获得节点dataformat原始值
/// </summary>
/// <param name="element">节实体</param>
/// <returns>获得节点dataformat原始值,返回""则未配置</returns>
public static string GetDataFormat(XElement element)
{
return GetAttributeOriginal(element, "dataformat");
}
}
}

欢迎访问:http://121.18.78.216 适易查询分析、工作流、内容管理及项目管理演示平台

你可能感兴趣的:(LINQ)