Beispiel #1
0
        public static Relationships Load(Package package, string source, Stream xml)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(xml);

            XmlElement typesRoot = null;

            // get Types root element
            foreach (var node in xmlDoc.ChildNodes)
            {
                // ignore if not an element
                var elt = node as XmlElement;
                if (elt == null)
                {
                    continue;
                }

                if (string.Equals(elt.NamespaceURI, Namespaces.Relationships, StringComparison.Ordinal) &&
                    string.Equals(elt.LocalName, "Relationships"))
                {
                    typesRoot = elt;
                    break;
                }
            }

            if (typesRoot == null)
            {
                throw new Exception("Invalid [Content_Types].xml. Missing Types root element.");
            }

            var rels = new Relationships(package, source);

            // now parse content types
            foreach (XmlNode node in typesRoot.ChildNodes)
            {
                var elt = node as XmlElement;
                // ignore if not an element
                if (elt == null)
                {
                    continue;
                }

                // ignore if not the right namespace
                if (!string.Equals(elt.NamespaceURI, Namespaces.Relationships, StringComparison.Ordinal) ||
                    !string.Equals(elt.LocalName, "Relationship", StringComparison.Ordinal))
                {
                    continue;
                }

                // read target mode; default to internal
                bool external = string.Equals(elt.GetAttribute("TargetMode"), "External", StringComparison.Ordinal);

                // create the rel
                var rel = new Relationship(
                    package,
                    source,
                    elt.GetAttribute("Id"),
                    elt.GetAttribute("Type"),
                    elt.GetAttribute("Target"),
                    external);

                rels.Add(rel.Id, rel);
            }

            return(rels);
        }