Esempio n. 1
0
        private static List <MasterMap> getMasterMaps(PackagePart pkgPart, PackageItem pkgItem)
        {
            List <MasterMap> masterMaps         = new List <MasterMap>();
            MasterMap        masterMap          = null;
            XDocument        packagePartContent = null;

            Console.WriteLine("--------------------------- Elements ---------------------------------------");
            packagePartContent = getPckagePartContent(pkgPart);
            if (packagePartContent != null)
            {
                /*XmlReader reader = packagePartContent.CreateReader();
                 * XmlNamespaceManager namespaceManager = new XmlNamespaceManager(reader.NameTable);
                 * namespaceManager.AddNamespace("aw", "http://schemas.microsoft.com/office/visio/2012/main");
                 * namespaceManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");*/

                foreach (var element in packagePartContent.Descendants().Where(el => el.Name.LocalName.Equals("Master")))
                {
                    masterMap = new MasterMap();
                    if (element.Attribute("ID") != null)
                    {
                        masterMap.Id = element.Attribute("ID").Value;
                    }
                    if (element.Attribute("Name") != null)
                    {
                        masterMap.Name = element.Attribute("Name").Value;
                    }
                    if (element.Attribute("UniqueID") != null)
                    {
                        masterMap.UniqueId = element.Attribute("UniqueID").Value;
                    }
                    var relElement = element.Descendants().Where(el => el.Name.LocalName.Equals("Rel")).FirstOrDefault();
                    if (relElement != null)
                    {
                        masterMap.RefId = relElement.FirstAttribute.Value.Trim();
                    }

                    if (masterMap != null)
                    {
                        masterMaps.Add(masterMap);
                    }
                }
            }

            return(masterMaps);
        }
Esempio n. 2
0
        private static Page getPages(PackagePart pkgPart, PackageItem pkgItem)
        {
            Page page = new Page {
                Uri = pkgPart.Uri.ToString(), ContentType = pkgPart.ContentType
            };

            PackagePartItem packagePartItem    = null;
            XDocument       packagePartContent = null;

            Console.WriteLine("--------------------------- Elements ---------------------------------------");
            packagePartContent = getPckagePartContent(pkgPart);
            if (packagePartContent != null)
            {
                //packagePartItem = new PackagePartItem { Uri = pkgPart.Uri.ToString(), ContentType = pkgPart.ContentType };

                XmlReader           reader           = packagePartContent.CreateReader();
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(reader.NameTable);
                namespaceManager.AddNamespace("aw", "http://schemas.microsoft.com/office/visio/2012/main");
                namespaceManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

                foreach (var element in packagePartContent.Descendants().Where(el => el.Name.LocalName.Equals("Shape")))
                {
                    try
                    {
                        if (element.Attribute("Name") != null && element.Attribute("Master") != null)
                        {
                            packagePartItem = new PackagePartItem {
                                ContentType = pkgPart.ContentType
                            };
                            Console.WriteLine("\tElement Id: {0} | Type: {1} | Element [Name]: {2} | Master: {3}",
                                              element.Attribute("ID").Value, element.Attribute("Type").Value,
                                              element.Attribute("Name").Value, element.Attribute("Master").Value);

                            packagePartItem.ID     = element.Attribute("ID").Value;
                            packagePartItem.Type   = element.Attribute("Type").Value;
                            packagePartItem.Name   = element.Attribute("Name").Value;
                            packagePartItem.Master = element.Attribute("Master").Value;

                            MasterMap masterMap = pkgItem.MasterMaps.Where(mm => mm.Id == packagePartItem.Master).FirstOrDefault();
                            if (masterMap != null)
                            {
                                packagePartItem.MasterItem = pkgItem.Masters.FirstOrDefault(m => m.RefId == masterMap.RefId);
                            }

                            packagePartItem.Uri = string.Format("/visio/masters/master{0}.xml", packagePartItem.Master);

                            //packagePartItem.Properties.Add("ID", element.Attribute("ID").Value);
                            //packagePartItem.Properties.Add("Type", element.Attribute("Type").Value);
                            //packagePartItem.Properties.Add("Name", element.Attribute("Name").Value);
                            //packagePartItem.Properties.Add("Master", element.Attribute("Master").Value);

                            //http://schemas.microsoft.com/office/visio/2012/main
                            var selectedElements = element.XPathSelectElements("./aw:Section[@N='Property']/aw:Row/aw:Cell", namespaceManager);
                            //int indx = 0;
                            string parkedValue = null;
                            foreach (var selectedElement in selectedElements)
                            {
                                bool isKeyFound = true;
                                if (selectedElement.Attribute("N") != null &&
                                    (selectedElement.Attribute("N").Value.Equals("Value") ||
                                     selectedElement.Attribute("N").Value.Equals("Label")))
                                {
                                    string key = selectedElement.Parent.FirstAttribute.Value;
                                    if (packagePartItem.MasterItem != null && packagePartItem.MasterItem.Properties.ContainsKey(key))
                                    {
                                        key = packagePartItem.MasterItem.Properties[key];
                                    }
                                    //key += "_" + packagePartItem.MasterItem.Properties[key];
                                    else
                                    {
                                        isKeyFound = false;
                                        Console.WriteLine("Key Not found " + key);

                                        if (selectedElement.Attribute("N").Value.Equals("Label"))
                                        {
                                            key        = selectedElement.Attribute("V").Value;
                                            isKeyFound = true;
                                        }
                                    }

                                    if (selectedElement.Attribute("V") != null)
                                    {
                                        Console.WriteLine("\t\tCell Value : {0}", selectedElement.Attribute("V").Value);

                                        if (selectedElement.Attribute("N").Value.Equals("Value"))
                                        {
                                            parkedValue = selectedElement.Attribute("V").Value.Replace("\n", "");
                                        }

                                        if (isKeyFound && parkedValue != null)
                                        {
                                            if (!packagePartItem.Properties.ContainsKey(key))
                                            {
                                                packagePartItem.Properties.Add(key, parkedValue);
                                            }
                                            parkedValue = null;
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("\t\tCell Value : {0}", selectedElement.Value);
                                        if (selectedElement.Attribute("N").Value.Equals("Value"))
                                        {
                                            parkedValue = selectedElement.Value.Replace("\n", "");
                                        }

                                        if (isKeyFound && parkedValue != null)
                                        {
                                            if (!packagePartItem.Properties.ContainsKey(key))
                                            {
                                                packagePartItem.Properties.Add(key, parkedValue);
                                            }
                                            parkedValue = null;
                                        }
                                    }
                                }
                            }

                            page.PackagePartItems.Add(packagePartItem);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            Console.WriteLine("--------------------------- End Of Elements ---------------------------------------");

            return(page);
        }