GetXmlFromUri() private method

Get the XmlDocument from an URI
private GetXmlFromUri ( Uri uri ) : XmlDocument
uri System.Uri The Uri to the part
return System.Xml.XmlDocument
示例#1
0
 /// <summary>
 /// Read shared strings to list
 /// </summary>
 private void GetSharedStrings()
 {
     if (_package.Package.PartExists(SharedStringsUri))
     {
         var         xml = _package.GetXmlFromUri(SharedStringsUri);
         XmlNodeList nl  = xml.SelectNodes("//d:sst/d:si", NameSpaceManager);
         _sharedStringsList = new List <SharedStringItem>();
         if (nl != null)
         {
             foreach (XmlNode node in nl)
             {
                 XmlNode n = node.SelectSingleNode("d:t", NameSpaceManager);
                 if (n != null)
                 {
                     _sharedStringsList.Add(new SharedStringItem()
                     {
                         Text = ExcelDecodeString(n.InnerText)
                     });
                 }
                 else
                 {
                     _sharedStringsList.Add(new SharedStringItem()
                     {
                         Text = node.InnerXml, isRichText = true
                     });
                 }
             }
         }
     }
 }
示例#2
0
		/// <summary>
		/// Read shared strings to list
		/// </summary>
		private void GetSharedStrings()
		{
			if (_package.Package.PartExists(SharedStringsUri))
			{
				var xml = _package.GetXmlFromUri(SharedStringsUri);
				XmlNodeList nl = xml.SelectNodes("//d:sst/d:si", NameSpaceManager);
				_sharedStringsList = new List<SharedStringItem>();
				if (nl != null)
				{
					foreach (XmlNode node in nl)
					{
						XmlNode n = node.SelectSingleNode("d:t", NameSpaceManager);
						if (n != null)
						{
                            _sharedStringsList.Add(new SharedStringItem() { Text = ConvertUtil.ExcelDecodeString(n.InnerText) });
						}
						else
						{
							_sharedStringsList.Add(new SharedStringItem() { Text = node.InnerXml, isRichText = true });
						}
					}
				}
                //Delete the shared string part, it will be recreated when the package is saved.
                foreach (var rel in Part.GetRelationships())
                {
                    if (rel.TargetUri.OriginalString.EndsWith("sharedstrings.xml", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Part.DeleteRelationship(rel.Id);
                        break;
                    }
                }                
                _package.Package.DeletePart(SharedStringsUri); //Remove the part, it is recreated when saved.
			}
		}
示例#3
0
        private XmlDocument GetXmlDocument(string startXml, Uri uri, string contentType, string relationship)
        {
            XmlDocument xmlDoc;

            if (_package.Package.PartExists(uri))
            {
                xmlDoc = _package.GetXmlFromUri(uri);
            }
            else
            {
                xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(startXml);

                // Create a the part and add to the package
                Packaging.ZipPackagePart part = _package.Package.CreatePart(uri, contentType);

                // Save it to the package
                StreamWriter stream = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write));
                xmlDoc.Save(stream);
                //stream.Close();
                _package.Package.Flush();

                // create the relationship between the workbook and the new shared strings part
                _package.Package.CreateRelationship(UriHelper.GetRelativeUri(new Uri("/xl", UriKind.Relative), uri), Packaging.TargetMode.Internal, relationship);
                _package.Package.Flush();
            }
            return(xmlDoc);
        }
示例#4
0
        /// <summary>
        /// Create or read the XML for the workbook.
        /// </summary>
        private void CreateWorkbookXml(XmlNamespaceManager namespaceManager)
        {
            if (_package.Package.PartExists(WorkbookUri))
            {
                _workbookXml = _package.GetXmlFromUri(WorkbookUri);
            }
            else
            {
                // create a new workbook part and add to the package
                Packaging.ZipPackagePart partWorkbook = _package.Package.CreatePart(WorkbookUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", _package.Compression);

                // create the workbook
                _workbookXml = new XmlDocument(namespaceManager.NameTable);

                _workbookXml.PreserveWhitespace = ExcelPackage.preserveWhitespace;
                // create the workbook element
                XmlElement wbElem = _workbookXml.CreateElement("workbook", ExcelPackage.schemaMain);

                // Add the relationships namespace
                wbElem.SetAttribute("xmlns:r", ExcelPackage.schemaRelationships);

                _workbookXml.AppendChild(wbElem);

                // create the bookViews and workbooks element
                XmlElement bookViews = _workbookXml.CreateElement("bookViews", ExcelPackage.schemaMain);
                wbElem.AppendChild(bookViews);
                XmlElement workbookView = _workbookXml.CreateElement("workbookView", ExcelPackage.schemaMain);
                bookViews.AppendChild(workbookView);

                // save it to the package
                StreamWriter stream = new StreamWriter(partWorkbook.GetStream(FileMode.Create, FileAccess.Write));
                _workbookXml.Save(stream);
                //stream.Close();
                _package.Package.Flush();
            }
        }