public void ToQName()
        {
            const string content = @"<xsl:stylesheet version='1.0'
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
	xmlns:bi='http://schemas.microsoft.com/Sql/2008/05/TableOp/dbo/BatchItems'
	xmlns:tt='http://schemas.microsoft.com/Sql/2008/05/Types/Tables/dbo'
	xmlns:bts='http:=//schemas.microsoft.com/BizTalk/2003/system-properties'
	xmlns:tp='urn:schemas.stateless.be:biztalk:properties:tracking:2012:04'>
</xsl:stylesheet>";

            var nsm = new XPathDocument(new StringReader(content)).CreateNavigator();

            nsm.MoveToFollowing(XPathNodeType.Element);

            Assert.That(
                "no-namespace".ToQName(nsm),
                Is.EqualTo(new XmlQualifiedName("no-namespace", null)));
            Assert.That(
                "bts:OutboundTransportLocation".ToQName(nsm),
                Is.EqualTo(new XmlQualifiedName("OutboundTransportLocation", "http:=//schemas.microsoft.com/BizTalk/2003/system-properties")));
            Assert.That(
                "tp:MessagingStepActivityID".ToQName(nsm),
                Is.EqualTo(new XmlQualifiedName("MessagingStepActivityID", "urn:schemas.stateless.be:biztalk:properties:tracking:2012:04")));
            Assert.That(
                () => ":MessagingStepActivityID".ToQName(nsm),
                Throws.ArgumentException.With.Message.EqualTo("':MessagingStepActivityID' is not a valid XML qualified name.\r\nParameter name: qName"));
        }
 private XPathNavigator BuildNavigator()
 {
     using (var stringReader = new StringReader(_transformBase.XmlContent))
     {
         var navigator = new XPathDocument(stringReader).CreateNavigator();
         navigator.MoveToFollowing(XPathNodeType.Element);
         return(navigator);
     }
 }
 /// <summary>
 /// Add all the namespaces and their prefixes declared in a given <see cref="TransformBase"/>-derived the XSLT transform.
 /// </summary>
 /// <typeparam name="TTransform">
 /// The <see cref="TransformBase"/>-derived XSLT transform to get the namespaces to declare from.
 /// </typeparam>
 /// <param name="xmlNamespaceManager">
 /// The <see cref="XmlNamespaceManager"/> to which to add the namespace declared in the given <typeparamref name="TTransform"/> <see cref="TransformBase"/> transform.
 /// </param>
 /// <returns>
 /// The <see cref="XmlNamespaceManager"/> to which the namespace declarations have been added.
 /// </returns>
 public static XmlNamespaceManager AddNamespaces <TTransform>(this XmlNamespaceManager xmlNamespaceManager)
     where TTransform : TransformBase, new()
 {
     using (var sr = new StringReader(new TTransform().XmlContent))
     {
         var navigator = new XPathDocument(sr).CreateNavigator();
         navigator.MoveToFollowing(XPathNodeType.Element);
         navigator.GetNamespacesInScope(XmlNamespaceScope.All).Each(n => xmlNamespaceManager.AddNamespace(n.Key, n.Value));
     }
     return(xmlNamespaceManager);
 }
예제 #4
0
        /// <summary>
        /// Deserializes the stream using a custom structure.
        /// </summary>
        /// <param name="stream">The stream containing the watter supply point data.</param>
        /// <returns>A dictionary containing the water supply points and their corresponding distances to the origin.</returns>
        private static List <WaterSupplyPoint> DeserializeCustomData(Stream stream)
        {
            var waterSupplyPoints = new List <WaterSupplyPoint>();

            // Read data using XPath
            const string namespaceURI = "";
            var          nav          = new XPathDocument(stream).CreateNavigator();

            while (nav.MoveToFollowing("WaterSupplyPoint", namespaceURI))
            {
                int distance = 0;
                var point    = new WaterSupplyPoint();

                // Coordinate
                nav.MoveToFollowing("Latitude", namespaceURI);
                double lat = nav.ValueAsDouble;
                nav.MoveToFollowing("Longitude", namespaceURI);
                double lng = nav.ValueAsDouble;
                point.Coordinate = new Coordinate(lat, lng);

                // Kind
                bool found = nav.MoveToFollowing("Kind", namespaceURI);
                if (found)
                {
                    switch (nav.Value)
                    {
                    case "pillarhydrant":
                        point.Kind = WaterSupplyPointKind.PillarHydrant;
                        break;

                    case "undergroundhydrant":
                        point.Kind = WaterSupplyPointKind.UndergroundHydrant;
                        break;

                    case "fireextinguishingpool":
                        point.Kind = WaterSupplyPointKind.FireExtinguishingPool;
                        break;

                    case "suctionpoint":
                        point.Kind = WaterSupplyPointKind.SuctionPoint;
                        break;

                    default:
                        point.Kind = WaterSupplyPointKind.Undefined;
                        break;
                    }
                }

                found = nav.MoveToFollowing("Location", namespaceURI);
                if (found)
                {
                    point.Location = nav.Value;
                }

                found = nav.MoveToFollowing("Information", namespaceURI);
                if (found)
                {
                    point.Information = nav.Value;
                }

                found = nav.MoveToFollowing("Size", namespaceURI);
                if (found && !string.IsNullOrWhiteSpace(nav.Value))
                {
                    point.Size = nav.ValueAsInt;
                }

                found = nav.MoveToFollowing("Connections", namespaceURI);
                if (found)
                {
                    point.Connections = nav.Value;
                }

                found = nav.MoveToFollowing("Distance", namespaceURI);
                if (found && !string.IsNullOrWhiteSpace(nav.Value))
                {
                    distance = nav.ValueAsInt;
                }

                point.Distance = distance;

                waterSupplyPoints.Add(point);
            }

            return(waterSupplyPoints);
        }