49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Sarand.Net.Standard.Base.Utils
|
|
{
|
|
//=======================================================================
|
|
public static class XmlUtil
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Convert XML document to string.
|
|
public static String GetXml (this XDocument xmlDoc)
|
|
{
|
|
using (var stringWriter = new StringWriter())
|
|
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
|
|
{
|
|
xmlDoc.WriteTo(xmlTextWriter);
|
|
xmlTextWriter.Flush();
|
|
return stringWriter.GetStringBuilder().ToString();
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Append element to node.
|
|
public static XmlElement AppendElement (this XmlNode node, String name, String text = "")
|
|
{
|
|
XmlDocument doc = (node is XmlDocument ? node as XmlDocument : node.OwnerDocument);
|
|
XmlElement child = (XmlElement)node.AppendChild(doc.CreateElement(name));
|
|
if (!StringUtil.IsEmpty(text))
|
|
child.AppendChild(doc.CreateTextNode(text));
|
|
return child;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get child text.
|
|
public static String GetChildText (this XmlNode node, String name)
|
|
{
|
|
foreach (XmlNode child in node.ChildNodes)
|
|
if (child.Name.EqualsCI(name))
|
|
return child.InnerText;
|
|
return String.Empty;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|