|
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;
namespace ESB.ExceptionHandling.ServiceModel
{
internal class Utilities
{
public static XmlDocument GetXmlDocument(string xml)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
return xmlDocument;
}
/// <summary>
/// Compacts the given XML by removing unneeded line breaks and formatting.
/// </summary>
/// <param name="xml">The XML to flatten.</param>
/// <returns> XML with removed unneeded line breaks and formatting.</returns>
public static string FlattenXml(string xml)
{
try
{
XmlDocument xmlDocument = GetXmlDocument(xml);
StringBuilder stringBuilder = new StringBuilder(xmlDocument.OuterXml.Length * 2);
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
//Format the XML to one line
xmlWriterSettings.OmitXmlDeclaration = true;
xmlWriterSettings.Encoding = Encoding.ASCII;
xmlWriterSettings.Indent = false;
xmlWriterSettings.NewLineHandling = NewLineHandling.None;
using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
{
xmlDocument.WriteTo(xmlWriter);
}
stringBuilder.Replace("\n", "");
return stringBuilder.ToString();
}
catch { }
return xml;
}
#region GetXPathNavigator Methods
/// <summary>
/// Get an XPath Navigator object for the XML data.
/// </summary>
/// <param name="xml">The XML data to get the XPath Navigator for.</param>
/// <returns>An XPath Navigator object for the XML data.</returns>
public static XPathNavigator GetXPathNavigator(string xml)
{
using (MemoryStream inputMemoryStream = new MemoryStream())
{
Encoding encoding = new ASCIIEncoding();
//Convert the XML into a byte array
byte[] byteArray = encoding.GetBytes(xml);
//Load the XML byte array into a memory stream
inputMemoryStream.Write(byteArray, 0, byteArray.Length);
//Set the postition of the memory stream to the beginning of the data
inputMemoryStream.Position = 0;
XPathDocument xPathDocument = new XPathDocument(inputMemoryStream);
return xPathDocument.CreateNavigator();
}
}
/// <summary>
/// Get an XPath Navigator object for the XML Document.
/// </summary>
/// <param name="xmlDocument">The XML Document to get the XPath Navigator for.</param>
/// <returns>An XPath Navigator object for the XML Document.</returns>
public static XPathNavigator GetXPathNavigator(XmlDocument xmlDocument)
{
XPathDocument xPathDocument = new XPathDocument(new XmlNodeReader(xmlDocument));
return xPathDocument.CreateNavigator();
}
#endregion
#region GetXPathXmlNode
/// <summary>
/// Gets the first XmlNode in the XML Document that can be found via the XPath expression.
/// </summary>
/// <param name="xml">The XML to search.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <returns>The first XmlNode in the XML Document that can be found via the XPath expression.</returns>
public static XmlNode GetXPathXmlNode(string xml, string xPathExpression)
{
XPathNavigator xPathNavigator = GetXPathNavigator(xml);
return GetXPathXmlNode(xPathNavigator, xPathExpression);
}
/// <summary>
/// Gets the first XmlNode in the XML Document that can be found via the XPath expression.
/// </summary>
/// <param name="xmlDocument">The XML Document to get the XPath Navigator for.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <returns>The first XmlNode in the XML Document that can be found via the XPath expression.</returns>
public static XmlNode GetXPathXmlNode(XmlDocument xmlDocument, string xPathExpression)
{
XPathNavigator xPathNavigator = GetXPathNavigator(xmlDocument);
return GetXPathXmlNode(xPathNavigator, xPathExpression);
}
/// <summary>
/// Gets the first XmlNode in the XML Document that can be found via the XPath expression.
/// </summary>
/// <param name="xPathNavigator">The XPath navigator to use to search the xml with.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <returns>The first XmlNode in the XML Document that can be found via the XPath expression.</returns>
public static XmlNode GetXPathXmlNode(XPathNavigator xPathNavigator, string xPathExpression)
{
XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
while (xPathNodeIterator.MoveNext())
{
if (xPathNodeIterator.Current.IsNode)
return (xPathNodeIterator.Current as IHasXmlNode).GetNode();
}
return null;
}
#endregion
#region GetXPathValue
/// <summary>
/// Gets the value of the first field in the XML that can be found via the XPath expression.
/// </summary>
/// <param name="xml">The XML to search.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <returns>The value of the first field in the XML that can be found via the XPath expression.</returns>
public static string GetXPathValue(string xml, string xPathExpression)
{
XPathNavigator xPathNavigator = GetXPathNavigator(xml);
return GetXPathValue(xPathNavigator, xPathExpression);
}
/// <summary>
/// Gets the value of the first field in the XML Document that can be found via the XPath expression.
/// </summary>
/// <param name="xmlDocument">The XML Document to get the XPath Navigator for.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <returns>The value of the first field in the XML Document that can be found via the XPath expression.</returns>
public static string GetXPathValue(XmlDocument xmlDocument, string xPathExpression)
{
XPathNavigator xPathNavigator = GetXPathNavigator(xmlDocument);
return GetXPathValue(xPathNavigator, xPathExpression);
}
/// <summary>
/// Gets the value of the first field in the XML that can be found via the XPath expression.
/// </summary>
/// <param name="xPathNavigator">The XPath navigator to use to search the xml with.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <returns>The value of the first field in the XML that can be found via the XPath expression.</returns>
public static string GetXPathValue(XPathNavigator xPathNavigator, string xPathExpression)
{
return GetXPathValue(xPathNavigator, xPathExpression, null);
}
/// <summary>
/// Gets the value of the first field in the XML that can be found via the XPath expression. If field is found, then the default is returned.
/// </summary>
/// <param name="xPathNavigator">The XPath navigator to use to search the xml with.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <param name="defaultValue">the default value to be returned if the XPath cannot be found.</param>
/// <returns>The value of the first field in the XML that can be found via the XPath expression otherwise the default value.</returns>
public static string GetXPathValue(XPathNavigator xPathNavigator, string xPathExpression, string defaultValue)
{
XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
while (xPathNodeIterator.MoveNext())
{
if (xPathNodeIterator.Current.HasChildren)
return xPathNodeIterator.Current.InnerXml;
else
return xPathNodeIterator.Current.Value;
}
return defaultValue;
}
/// <summary>
/// Gets the value of the first field in the XML that can be found via the XPath expression. If field is found, then the default is returned.
/// </summary>
/// <param name="xPathNavigator">The XPath navigator to use to search the xml with.</param>
/// <param name="xPathExpression">The XPath expression to search with.</param>
/// <param name="defaultValue">the default value to be returned if the XPath cannot be found.</param>
/// <returns>The value of the first field in the XML that can be found via the XPath expression otherwise the default value.</returns>
public static int GetXPathValue(XPathNavigator xPathNavigator, string xPathExpression, int defaultValue)
{
XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
while (xPathNodeIterator.MoveNext())
return xPathNodeIterator.Current.ValueAsInt;
return defaultValue;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have almost 20 years commercial software development using a number of languages from C++ to C#/VB. My main focus has been in the Microsoft space and, since the early 2000's, most of this has been in the web or integration arena. Over the past few years I have been focusing on building business solutions using a variety of products such as BizTalk and Share Point.
In terms of SDLC's, I have experience with various methodologies such as Agile, RUP, Iterative and Waterfall.
Lastly, the roles I have had in the last few years have given me the opportunity to gain a lot of experience as a consultant. Since, over the last 18 years, I have worked with many customers this has further given me the chance to enjoy many and varying challenges that have helped me grow in my career.
Today, I spend a lot of time talking, designing, documenting and mentoring team members. I also spend quite a bit of time authoring software and find the combination a perfect fit for me.
Specialties
Consultancy, Solution architecture, Solution design, Project costing and tracking, Team lead, Software development, C#, VB, ASP.NET, HTML, DHTML, JavaScript, BizTalk, SharePoint