示例#1
0
        private bool CompareResult(XmlNode testCase, string sourceFile, object value, bool xmlCompare)
        {
            string id       = ((XmlElement)testCase).GetAttribute("name");
            bool   isSingle = false;
            bool   isExcpt  = (id == "fn-union-node-args-005") ||
                              (id == "fn-union-node-args-006") || (id == "fn-union-node-args-007") ||
                              (id == "fn-union-node-args-009") || (id == "fn-union-node-args-010") ||
                              (id == "fn-union-node-args-011");

            if (id == "ReturnExpr010")
            {
                xmlCompare = true;
            }
            if (id != "CondExpr012" && id != "NodeTest006")
            {
                if (value is XPathItem)
                {
                    isSingle = true;
                }
                else if (value is XPath2NodeIterator)
                {
                    XPath2NodeIterator iter = (XPath2NodeIterator)value;
                    isSingle = iter.IsSingleIterator;
                }
            }
            XmlDocument doc1 = new XmlDocument(_nameTable);

            if (xmlCompare)
            {
                doc1.Load(sourceFile);
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<?xml version='1.0'?>");
                sb.Append("<root>");
                TextReader textReader = new StreamReader(sourceFile, true);
                sb.Append(textReader.ReadToEnd());
                textReader.Close();
                sb.Append("</root>");
                doc1.LoadXml(sb.ToString());
            }
            MemoryStream ms     = new MemoryStream();
            XmlWriter    writer = new XmlTextWriter(ms, Encoding.UTF8);

            if (!(xmlCompare && isSingle || isExcpt))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(doc1.DocumentElement.Name, "");
            }
            if (value is XPath2NodeIterator)
            {
                bool string_flag = false;
                foreach (XPathItem item in (XPath2NodeIterator)value)
                {
                    if (item.IsNode)
                    {
                        XPathNavigator nav = (XPathNavigator)item;
                        if (nav.NodeType == XPathNodeType.Attribute)
                        {
                            writer.WriteStartAttribute(nav.Prefix, nav.LocalName, nav.NamespaceURI);
                            writer.WriteString(nav.Value);
                            writer.WriteEndAttribute();
                        }
                        else
                        {
                            writer.WriteNode(nav, false);
                        }
                        string_flag = false;
                    }
                    else
                    {
                        if (string_flag)
                        {
                            writer.WriteString(" ");
                        }
                        writer.WriteString(item.Value);
                        string_flag = true;
                    }
                }
            }
            else
            if (value is XPathItem)
            {
                XPathItem item = (XPathItem)value;
                if (item.IsNode)
                {
                    writer.WriteNode((XPathNavigator)item, false);
                }
                else
                {
                    writer.WriteString(item.Value);
                }
            }
            else
            {
                if (value != Undefined.Value)
                {
                    writer.WriteString(XPath2Convert.ToString(value));
                }
            }
            if (!(xmlCompare && isSingle || isExcpt))
            {
                writer.WriteEndElement();
            }
            writer.Flush();
            ms.Position = 0;
            XmlDocument doc2 = new XmlDocument(_nameTable);

            doc2.Load(ms);
            writer.Close();
            TreeComparer comparer = new TreeComparer();

            comparer.IgnoreWhitespace = true;
            bool res = comparer.DeepEqual(doc1.CreateNavigator(), doc2.CreateNavigator());

            return(res);
        }