本文整理汇总了C#中System.Xml.Linq.XDocument.Normalize方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.Normalize方法的具体用法?C# XDocument.Normalize怎么用?C# XDocument.Normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XDocument
的用法示例。
在下文中一共展示了XDocument.Normalize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssertByLine
public static void AssertByLine(this XDocument expected, XDocument actual)
{
// MH comparing XML by string sucks... but this is usable for my porpoises
var str1 = expected.Normalize().ToString();
var str2 = actual.Normalize().ToString();
var expectedLines = str1.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
var actualLines = str2.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
var smallest = Math.Min(expectedLines.Count(), actualLines.Count());
for (var i = 0; i < smallest; i++)
Assert.That(
expectedLines[i].Equals(actualLines[i], StringComparison.OrdinalIgnoreCase),
string.Format("Xml differs at line {0}.\r\n{1}\r\n{2}", (i+1), expectedLines[i], actualLines[i]));
Assert.AreEqual(expectedLines.Count(), actualLines.Count(), "Xml has a different number of lines");
}
示例2: CheckXml
/// <summary>
/// Check an XElement against some expected XML.
/// <para>
/// The supplied node is copied and normalized to get actual differences.
/// </para>
/// </summary>
/// <param name="expected">XElement to use</param>
/// <param name="candidate">XML to compare against.</param>
/// <param name="fullComparison">Whether to do a full comparison using prefix/namespace</param>
/// <param name="diffFilePath">Where/whether to write out the HTML difference</param>
public static string CheckXml(this XElement expected, XElement candidate, bool fullComparison = true, string diffFilePath = "")
{
// Xml's are normalized before comparision, so that we can get actual difference when we turn on Namespaces & Prefix's differnces
var source = new XDocument(expected);
source = source.Normalize();
var expectedNode = source.ToXmlDocument().SelectSingleNode("/*");
var target = new XDocument(candidate);
target = target.Normalize();
var candidateNode = target.ToXmlDocument().SelectSingleNode("/*");
using (var stringWriter = new StringWriter())
{
bool areDifferent;
using (var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented })
{
// Default engine ignores prefix/namespace so we need to enable if we are doing a full comparison.
var diff = DefaultDiffEngine();
diff.IgnorePrefixes = !fullComparison;
diff.IgnoreNamespaces = !fullComparison;
areDifferent = !diff.Compare(expectedNode, candidateNode, xmlWriter);
}
if (areDifferent)
{
if (!string.IsNullOrEmpty(diffFilePath))
{
var diffgram = stringWriter.GetStringBuilder().ToString();
WriteDiffFile(diffFilePath, diffgram, expectedNode);
}
return string.Format(
"Xml differs.{0}Expected:{0}{1}{0}{0}Actual:{0}{2}{0}{0}Diff:{0}{3}",
Environment.NewLine,
expectedNode.ToXmlString(),
candidateNode.ToXmlString(),
stringWriter);
}
}
return string.Empty;
}