当前位置: 首页>>代码示例>>C#>>正文


C# XDocument.Normalize方法代码示例

本文整理汇总了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");
        }
开发者ID:haboustak,项目名称:XdsKit,代码行数:17,代码来源:XDocumentExtensions.AssertByLine.cs

示例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;
        }
开发者ID:RustyF,项目名称:EnergyTrading-Core,代码行数:51,代码来源:XmlDiffFactory.cs


注:本文中的System.Xml.Linq.XDocument.Normalize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。