XmlHelper XML通用类
/// <summary>
/// 提供对XML数据的读写
/// </summary>
public sealed class XmlHelper
{
/// <summary>
/// 声明一个XmlDocument空对象
/// </summary>
private XmlDocument _xmlDoc = new XmlDocument();
/// <summary>
/// 构造函数,导入Xml文件
/// </summary>
/// <param name="xmlFile">文件绝对路径</param>
public XmlHelper(string xmlFile)
{
try
{
_xmlDoc.Load(xmlFile);
}
catch (Exception ex)
{
throw ex;
}
}
~XmlHelper()
{
_xmlDoc = null; // 释放XmlDocument对象
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="filePath">文件绝对路径</param>
public void Save(string filePath)
{
try
{
_xmlDoc.Save(filePath);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 根据Xml文件的节点路径,返回一个DataSet数据集
/// </summary>
/// <param name="xmlPathNode">Xml文件的某个节点</param>
/// <returns></returns>
public DataSet GetDataSet(string xmlPathNode)
{
DataSet ds = new DataSet();
try
{
System.IO.StringReader read = new System.IO.StringReader(_xmlDoc.SelectSingleNode(xmlPathNode).OuterXml);
ds.ReadXml(read);
read.Close();
}
catch (Exception ex)
{
string err = ex.Message;
}
return ds;
}
/// <summary>
/// 属性查询,返回属性值
/// </summary>
/// <param name="xmlPathNode">属性所在的节点</param>
/// <param name="attributeName">属性名</param>
/// <returns></returns>
public string GetAttributeValue(string xmlPathNode, string attributeName)
{
string rlt = string.Empty;
try
{
rlt = _xmlDoc.SelectSingleNode(xmlPathNode).Attributes[attributeName].Value;
}
catch (Exception ex)
{
string err = ex.Message;
}
return rlt;
}
/// <summary>
/// 节点查询,返回节点值
/// </summary>
/// <param name="xmlPathNode">节点的路径</param>
/// <returns></returns>
public string GetNodeText(string xmlPathNode)
{
string txt = _xmlDoc.SelectSingleNode(xmlPathNode).InnerText;
if (null == txt || string.Empty == txt)
{
return string.Empty;
}
return txt;
}
/// <summary>
/// 节点值查询判断
/// </summary>
/// <param name="xmlPathNode">父节点</param>
/// <param name="index">节点索引</param>
/// <param name="nodeText">节点值</param>
/// <returns></returns>
public bool IsValidNode(string xmlPathNode, int index, string nodeText)
{
try
{
XmlNodeList nodeList = _xmlDoc.SelectNodes(xmlPathNode);
// 循环遍历节点,查询是否存在该节点
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeText == nodeList[i].ChildNodes[index].InnerText)
{
return true;
}
}
}
catch (Exception ex)
{
string err = ex.Message;
}
return false;
}
/// <summary>
/// 获取子节点个数
/// </summary>
/// <param name="xmlPathNode">父节点</param>
/// <returns></returns>
public int NodeCount(string xmlPathNode)
{
int rlt = 0;
try
{
rlt = _xmlDoc.SelectSingleNode(xmlPathNode).ChildNodes.Count;
}
catch (Exception ex)
{
string err = ex.Message;
rlt = 0;
}
return rlt;
}
/// <summary>
/// 更新一个节点的内容
/// </summary>
/// <param name="xmlPathNode">节点的路径</param>
/// <param name="nodeContent">新的节点值</param>
/// <returns></returns>
public bool UpdateNode(string xmlPathNode, string nodeContent)
{
try
{
_xmlDoc.SelectSingleNode(xmlPathNode).InnerText = nodeContent;
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 更新N个节点值
/// </summary>
/// <param name="xmlParentNode">父节点</param>
/// <param name="xmlNodes">子节点</param>
/// <param name="nodeContents">子节点内容</param>
/// <returns></returns>
public bool UpdateNode(string xmlParentNode, string[] xmlNodes, string[] nodeContents)
{
try
{
// 根据节点数组循环修改节点值
for (int i = 0; i < xmlNodes.Length; i++)
{
_xmlDoc.SelectSingleNode(xmlParentNode + "/" + xmlNodes[i]).InnerText = nodeContents[i];
}
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 修改属性
/// </summary>
/// <param name="xmlPathNode">属性所在的节点</param>
/// <param name="attributeName">属性名</param>
/// <param name="attributeValue">属性值</param>
/// <returns></returns>
public bool UpdateAttributeValue(string xmlPathNode, string attributeName, string attributeValue)
{
try
{
_xmlDoc.SelectSingleNode(xmlPathNode).Attributes[attributeName].Value = attributeValue;
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 添加属性
/// </summary>
/// <param name="xmlPathNode">属性所在的节点</param>
/// <param name="attributeName">属性名</param>
/// <param name="attributeValue">属性值</param>
/// <returns></returns>
public bool InsertAttribute(string xmlPathNode, string attributeName, string attributeValue)
{
try
{
XmlElement element = (XmlElement)_xmlDoc.SelectSingleNode(xmlPathNode);
element.SetAttribute(attributeName, attributeValue);
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 插入一个节点,带N个子节点
/// </summary>
/// <param name="xmlPathNode">当前节点</param>
/// <param name="nodeName">插入节点的名称</param>
/// <param name="elementNames">子节点的名称数组</param>
/// <param name="contents">子节点的内容数组</param>
/// <returns></returns>
public bool InsertNode(string xmlPathNode, string nodeName, string[] elementNames, string[] contents)
{
try
{
XmlNode node = _xmlDoc.SelectSingleNode(xmlPathNode);
XmlElement childNode = _xmlDoc.CreateElement(nodeName);
node.AppendChild(childNode);
for (int i = 0; i < elementNames.Length; i++)
{
XmlElement element = _xmlDoc.CreateElement(elementNames[i]);
element.InnerText = contents[i];
childNode.AppendChild(element);
}
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 删除一个节点
/// </summary>
/// <param name="xmlPathNode">节点的路径</param>
/// <returns></returns>
public bool DeleteNode(string xmlPathNode)
{
try
{
_xmlDoc.SelectSingleNode(xmlPathNode).ParentNode.RemoveChild(_xmlDoc.SelectSingleNode(xmlPathNode));
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 将对象序列化成XML
/// </summary>
/// <param name="obj"></param>
/// <param name="hasNamespace">是否有Xml头部</param>
/// <returns></returns>
public static string Serialize(object obj, bool hasNamespace = false)
{
string xml = string.Empty;
try
{
var sb = new StringBuilder();
var xmlSettings = new XmlWriterSettings();
xmlSettings.OmitXmlDeclaration = hasNamespace;
// 换行缩进
xmlSettings.Indent = true;
xmlSettings.Encoding = System.Text.Encoding.Default;
using (var xmlWriter = XmlWriter.Create(sb, xmlSettings))
{
var xmlSerializer = new XmlSerializer(obj.GetType());
// 去除默认命名空间xmlns:xsd和xmlns:xsi
var xmlNampespace = new XmlSerializerNamespaces();
xmlNampespace.Add("", "");
if (hasNamespace)
{
xmlSerializer.Serialize(xmlWriter, obj);
}
else
{
xmlSerializer.Serialize(xmlWriter, obj, xmlNampespace);
}
xml = sb.ToString();
}
}
catch (Exception ex)
{
throw ex;
}
return xml;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="xmlStr">xml字符串</param>
/// <returns>反序列化模型</returns>
public static T DeSerialize<T>(string xmlStr)
{
return (T)Deserialize(typeof(T), xmlStr);
}
private static object Deserialize(Type type, string xmlStr)
{
try
{
using (var sr = new StringReader(xmlStr))
{
var serializer = new XmlSerializer(type);
return serializer.Deserialize(sr);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将XML文本写入指定文件
/// </summary>
/// <param name="filePath">绝对路径</param>
/// <param name="xmlStr">XML文本</param>
/// <returns></returns>
public static bool WriteXmlString(string filePath, string xmlStr)
{
try
{
File.WriteAllText(filePath, xmlStr);
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
/// <summary>
/// 读取XML文本
/// </summary>
/// <param name="filePath">绝对路径</param>
/// <returns></returns>
public static string ReadXmlString(string filePath)
{
try
{
return File.ReadAllText(filePath, Encoding.Default);
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
}
Demo
VersionInfo version = new VersionInfo()
{
AppVersion = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
AppInfo = info,
};
string xml = XmlHelper.Serialize(version);
XmlHelper.WriteXmlString(file, xml);
......
var versionObj = XmlHelper.DeSerialize<VersionInfo>(xml);
Extend
/// <summary>
/// 创建XML文件
/// </summary>
/// <param name="uri">xml文件路径</param>
/// <param name="rootName">根节点名称</param>
public static void CreateXmlFile(this string uri, string rootName)
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement(rootName);
doc.AppendChild(root);
doc.Save(uri);
}
XElement Value&Attribute
/// <summary>
/// 获取XML的节点值
/// </summary>
/// <param name="parent">父结点</param>
/// <param name="nodeName">结点名称</param>
/// <param name="defaultValue">结点默认值</param>
/// <returns></returns>
public static string GetXmlNodeValue(this XElement parent, string nodeName, string defaultValue = "")
{
if (parent == null || parent.Element(nodeName) == null)
{
return defaultValue;
}
return parent.Element(nodeName).Value;
}
/// <summary>
/// 获取XML的特性值
/// </summary>
/// <param name="node">当前节点</param>
/// <param name="attributeName">特性名称</param>
/// <param name="defaultValue">特性默认值</param>
/// <returns></returns>
public static string GetXmlAttributeValue(this XElement node, string attributeName, string defaultValue = "")
{
if (node == null || node.Attribute(attributeName) == null)
{
return defaultValue;
}
return node.Attribute(attributeName).Value;
}
获取 XElement
/// <summary>
/// 快速获取指定节点及子节点
/// </summary>
/// <param name="uri">xml文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <remarks>对于很大的XML文档,使用Stream读取文件;快速、非缓存、只读、仅向前的</remarks>
/// <returns></returns>
public static XElement GetXmlNode(this string uri, string nodeName)
{
XElement rslt = null;
using (XmlReader reader = XmlReader.Create(uri))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == nodeName)
{
rslt = XElement.ReadFrom(reader) as XElement;
break;
}
else
{
reader.Read();
}
}
}
return rslt;
}
/// <summary>
/// 获取指定节点的集合
/// </summary>
/// <param name="uri">xml文件路径</param>
/// <param name="elementName">节点名称</param>
/// <returns></returns>
public static IEnumerable<XElement> GetXmlNodes(this string uri, string elementName)
{
using (XmlReader reader = XmlReader.Create(uri))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == elementName)
{
var element = XElement.ReadFrom(reader) as XElement;
if (element != null)
{
yield return element;
}
}
else
{
reader.Read();
}
}
}
}
/// <summary>
/// 获取XML的特性值
/// </summary>
/// <param name="uri">xml文件路径</param>
/// <param name="nodeName">当前节点名称</param>
/// <param name="attributeName">特性名称</param>
/// <param name="defaultValue">特性默认值</param>
/// <returns></returns>
public static string GetXmlAttributeValue(this string uri, string nodeName, string attributeName, string defaultValue = "")
{
XElement element = uri.GetXmlNode(nodeName);
return element.GetXmlAttributeValue(attributeName, defaultValue);
}
string xmlPath = "....\info.xml"
XElement element = xmlPath.GetXmlNode("Type");
string value = element.GetXmlNodeValue("ChildType");
string attr = element.GetXmlAttributeValue("Name");
string attr2 = xmlPath.GetXmlAttributeValue("Type", "Name")
设置 XElement
/// <summary>
/// 设置XML的结点值
/// </summary>
/// <param name="parent">父节点</param>
/// <param name="nodeName">当前节点</param>
/// <param name="attributeName">节点特性名称</param>
/// <param name="value">特性值</param>
public static void SetXmlNodeValue(this XElement parent, string nodeName, string attributeName, object value)
{
if (parent == null)
{
return;
}
var node = parent.Element(nodeName);
if (node == null)
{
node = new XElement(nodeName, new XAttribute(attributeName, value));
parent.Add(node);
}
else
{
SetXmlAttributeValue(node, attributeName, value);
}
}
/// <summary>
/// 设置XML的结点值
/// </summary>
/// <param name="parent">父节点</param>
/// <param name="nodeName">节点名称</param>
/// <param name="value">节点值</param>
public static void SetXmlNodeValue(this XElement parent, string nodeName, object value)
{
if (parent == null)
{
return;
}
var node = parent.Element(nodeName);
if (node == null)
{
node = new XElement(nodeName, value);
parent.Add(node);
}
else
{
//node.Value = value.ToString();
node.ReplaceNodes(value);
}
}
/// <summary>
/// 设置XML的特性值
/// </summary>
/// <param name="node">当前节点</param>
/// <param name="attributeName">节点特性名称</param>
/// <param name="value">特性值</param>
public static void SetXmlAttributeValue(this XElement node, string attributeName, object value)
{
if (node == null)
{
return;
}
if (node.Attribute(attributeName) == null)
{
node.Add(new XAttribute(attributeName, value));
}
else
{
node.Attribute(attributeName).Value = value.ToString();
}
}
demo
NodeSet 是一个集合节点,不能有 XmlAttribute 特性,不然会解析失败;
string xmlPath = "....\\info.xml";
var root = xmlPath.GetXmlNode("RootName");
var tmp = root;
tmp.SetXmlNodeValue("Node1", "Value", 1);
tmp = tmp.Element("NodeSet");
var list = new List<XElement>();
foreach (var item in DemoList)
{
var node = new XElement("Node2");
node.SetXmlAttributeValue("attribute1", item.Attribute1);
node.SetXmlAttributeValue("attribute2", item.Attribute2);
node.SetXmlAttributeValue("attribute3", item.Attribute3);
list.Add(node);
}
tmp.SetXmlNodeValue("NodeSet", list);
root.Save(xmlFile);
升级版本
WSCommFunc.LongThread(async () =>
{
WSCommFunc.PrintThreadId("Sequence1");
xmlFile = @"C:\Users\WESSON\Desktop\test.xml";
var helper = await XmlHelper.LoadAsync(xmlFile, "Sequence").ConfigureAwait(false);
helper["Node1"] = 123;
helper["Node3", "a33"] = 134.678f;
helper.Save();
Console.WriteLine(helper["Node11", 80]);
Console.WriteLine(helper["Node3", "a33", ""]);
// 默认值
var val = helper["Node4"].ToString().Value<int>();
// 指定节点字符串值
var str = helper[(XName)"Node4", "text"].ToString();
// 集合
var node = (XElement)helper["Node5"];
if (node != null && node.HasElements)
{
foreach (var item in node.Elements())
{
var bb = item.GetAttributeValue("IsExist").Value<bool>();
}
}
});
public class XmlHelper
{
private XElement _root;
private string _xmlFile;
/// <summary>
/// 加载XML
/// </summary>
/// <param name="xmlFile">文件绝对路径</param>
/// <param name="rootName">根节点名称</param>
/// <returns></returns>
public static XmlHelper Load(string xmlFile, string rootName)
{
var rslt = new XmlHelper();
rslt.Initialize(xmlFile, rootName);
return rslt;
}
/// <summary>
/// 异步加载XML
/// </summary>
/// <param name="xmlFile">文件绝对路径</param>
/// <param name="rootName">根节点名称</param>
/// <returns></returns>
public static Task<XmlHelper> LoadAsync(string xmlFile, string rootName)
{
var rslt = new XmlHelper();
return rslt.InitializeAsync(xmlFile, rootName);
}
/// <summary>
/// 读入一个文件,并按XML的方式反序列化对象。
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="fileFullName">文件路径</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserializeFromFile<T>(string fileFullName, Encoding encoding)
{
if (String.IsNullOrEmpty(fileFullName))
{
throw new ArgumentNullException("fileFullName");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (!File.Exists(fileFullName))
{
throw new FileNotFoundException("fileFullName");
}
string xml = File.ReadAllText(fileFullName, encoding);
return XmlDeserialize<T>(xml, encoding);
}
/// <summary>
/// 从XML字符串中反序列化对象
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="str">包含对象的XML字符串</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserialize<T>(string str, Encoding encoding)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException("str");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(encoding.GetBytes(str)))
{
using (StreamReader sr = new StreamReader(ms, encoding))
{
try
{
return (T)mySerializer.Deserialize(sr);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
/// <summary>
/// 将一个对象按XML序列化的方式写入到一个文件
/// </summary>
/// <param name="obj">要序列化的对象</param>
/// <param name="fileFullName">保存文件路径</param>
/// <param name="encoding">编码方式</param>
public static void XmlSerializeToFile(object obj, string fileFullName, Encoding encoding)
{
if (String.IsNullOrEmpty(fileFullName))
{
throw new ArgumentNullException("fileFullName");
}
if (!Directory.Exists(fileFullName))
{
Directory.CreateDirectory(fileFullName);
}
using (FileStream file = new FileStream(fileFullName, FileMode.Create, FileAccess.Write))
{
XmlSerializeInternal(file, obj, encoding);
}
}
private static void XmlSerializeInternal(Stream stream, object obj, Encoding encoding)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.IndentChars = "\t";
/// 不生成声明头
settings.OmitXmlDeclaration = true;
/// 强制指定命名空间,覆盖默认的命名空间。
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, obj, namespaces);
writer.Close();
}
}
/// <summary>
/// 操作XML的节点
/// </summary>
/// <param name="nodeName">节点名称</param>
/// <param name="defaultValue">节点值</param>
/// <remarks>支持次级目录,集合设置</remarks>
/// <returns></returns>
public object this[XName nodeName, object defaultValue = null]
{
get
{
var node = GetXmlNode(_xmlFile, nodeName.LocalName);
if (node == null)
{
return defaultValue ?? "";
}
if (node.HasElements)
{
return node;
}
if (node.HasAttributes)
{
return node.FirstAttribute.Value;
}
return node.Value;
}
set
{
var node = _root.Element(nodeName);
if (node == null)
{
node = new XElement(nodeName, value);
_root.Add(node);
}
else
{
node.ReplaceNodes(value);
}
}
}
/// <summary>
/// 操作XML的特性
/// </summary>
/// <param name="nodeName">节点名称</param>
/// <param name="attributeName">节点特性名称</param>
/// <param name="defaultValue">特性值</param>
/// <remarks>支持次级目录</remarks>
/// <returns></returns>
public string this[string nodeName, string attributeName, string defaultValue]
{
get
{
var node = GetXmlNode(_xmlFile, nodeName);
return GetXmlAttributeValue(node, attributeName, defaultValue);
}
}
public object this[string nodeName, string attributeName]
{
set
{
var node = _root.Element(nodeName);
if (node == null)
{
node = new XElement(nodeName);
_root.Add(node);
}
SetXmlAttributeValue(node, attributeName, value);
}
}
/// <summary>
/// 保存文件
/// </summary>
public void Save()
{
try
{
_root.Save(_xmlFile);
}
catch (Exception ex)
{
throw ex;
}
}
private XmlHelper() { }
private async Task<XmlHelper> InitializeAsync(string xmlFile, string rootName)
{
await Task.Yield();
Initialize(xmlFile, rootName);
return this;
}
private void Initialize(string xmlFile, string rootName)
{
if (!File.Exists(xmlFile))
{
var doc = new XmlDocument();
var root = doc.CreateElement(rootName);
doc.AppendChild(root);
doc.Save(xmlFile);
}
_root = XElement.Load(xmlFile); // 加载全部时比较耗时,但又优于 GetXmlNode 方法
if (_root == null)
{
throw new ArgumentException("rootName");
}
_xmlFile = xmlFile;
}
/// <summary>
/// 获取XML的特性值
/// </summary>
/// <param name="node">当前节点</param>
/// <param name="attributeName">特性名称</param>
/// <param name="defaultValue">特性默认值</param>
/// <returns></returns>
private string GetXmlAttributeValue(XElement node, string attributeName, string defaultValue = "")
{
if (node == null || node.Attribute(attributeName) == null)
{
return defaultValue;
}
return node.Attribute(attributeName).Value;
}
/// <summary>
/// 设置XML的特性值
/// </summary>
/// <param name="node">当前节点</param>
/// <param name="attributeName">节点特性名称</param>
/// <param name="value">特性值</param>
private void SetXmlAttributeValue(XElement node, string attributeName, object value)
{
if (node == null)
{
return;
}
if (node.Attribute(attributeName) == null)
{
node.Add(new XAttribute(attributeName, value));
}
else
{
node.Attribute(attributeName).Value = value.ToString();
}
}
/// <summary>
/// 获取指定节点的集合
/// </summary>
/// <param name="xmlFile">xml文件路径</param>
/// <param name="elementName">节点名称</param>
/// <remarks>对于很大的XML文档,使用Stream读取文件;快速、非缓存、只读、仅向前的</remarks>
/// <returns></returns>
private IEnumerable<XElement> GetXmlNodes(string xmlFile, string elementName)
{
using (XmlReader reader = XmlReader.Create(xmlFile))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == elementName)
{
var element = XElement.ReadFrom(reader) as XElement;
if (element != null)
{
yield return element;
}
}
else
{
reader.Read();
}
}
}
}
/// <summary>
/// 快速获取指定节点及子节点
/// </summary>
/// <param name="xmlFile">xml文件路径</param>
/// <param name="nodeName"></param>
/// <remarks>对于很大的XML文档,使用Stream读取文件;快速、非缓存、只读、仅向前的</remarks>
/// <returns></returns>
private XElement GetXmlNode(string xmlFile, string nodeName)
{
XElement rslt = null;
HiPerfTimer.Execute(() =>
{
using (XmlReader reader = XmlReader.Create(xmlFile))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == nodeName)
{
rslt = XElement.ReadFrom(reader) as XElement;
break;
}
else
{
reader.Read();
}
}
}
}, nodeName);
return rslt;
}
}