本文整理汇总了C#中OpenXmlPart.GetStream方法的典型用法代码示例。如果您正苦于以下问题:C# OpenXmlPart.GetStream方法的具体用法?C# OpenXmlPart.GetStream怎么用?C# OpenXmlPart.GetStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenXmlPart
的用法示例。
在下文中一共展示了OpenXmlPart.GetStream方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadPart
private XDocument LoadPart(OpenXmlPart source)
{
if (source == null) return null;
var part = source.Annotation<XDocument>();
if (part != null) return part;
using (var str = source.GetStream())
using (var streamReader = new StreamReader(str))
using (var xr = XmlReader.Create(streamReader))
part = XDocument.Load(xr);
return part;
}
示例2: FlushPart
private static void FlushPart(OpenXmlPart part, HashSet<OpenXmlPart> visited)
{
visited.Add(part);
XDocument xdoc = part.Annotation<XDocument>();
if (xdoc != null && xdoc.Annotation<ChangedSemaphore>() != null)
{
using (XmlWriter xw = XmlWriter.Create(part.GetStream(FileMode.Create, FileAccess.Write)))
{
xdoc.Save(xw);
}
xdoc.RemoveAnnotations<ChangedSemaphore>();
xdoc.Changing += ElementChanged;
xdoc.Changed += ElementChanged;
}
foreach (IdPartPair item in part.Parts)
if (!visited.Contains(item.OpenXmlPart))
FlushPart(item.OpenXmlPart, visited);
}
示例3: GenerateImagePart
private static void GenerateImagePart(OpenXmlPart part, byte[] imageFileBytes)
{
// Write the contents of the image to the ImagePart.
using (BinaryWriter writer = new BinaryWriter(part.GetStream()))
{
writer.Write(imageFileBytes);
writer.Flush();
}
}
示例4: OpenXmlPartReader
/// <summary>
/// Initializes a new instance of the OpenXmlPartReader class using the supplied OpenXmlPart and Boolean values.
/// </summary>
/// <param name="openXmlPart">The OpenXmlPart to read.</param>
/// <param name="readMiscNodes">Specify false to indicate to the reader to skip all miscellaneous nodes. The default value is false.</param>
public OpenXmlPartReader(OpenXmlPart openXmlPart, bool readMiscNodes)
: this(readMiscNodes)
{
if (openXmlPart == null)
{
throw new ArgumentNullException("openXmlPart");
}
// set MaxCharactersInDocument to limit the part size on loading DOM.
this._elementContext.XmlReaderSettings.MaxCharactersInDocument = openXmlPart.MaxCharactersInPart;
Stream partStream = openXmlPart.GetStream(FileMode.Open);
this.Init(partStream, /*closeInput*/true);
}
示例5: LoadXDocument
private XDocument LoadXDocument(OpenXmlPart part)
{
XDocument xdoc;
using (StreamReader reader = new StreamReader(part.GetStream()))
xdoc = XDocument.Load(XmlReader.Create(reader));
return xdoc;
}
示例6: OpenXmlPartWriter
/// <summary>
/// Initializes a new instance of the OpenXmlPartWriter.
/// </summary>
/// <param name="openXmlPart">The OpenXmlPart to be written to.</param>
/// <param name="encoding">The encoding for the XML stream.</param>
public OpenXmlPartWriter(OpenXmlPart openXmlPart, Encoding encoding)
{
if (openXmlPart == null)
{
throw new ArgumentNullException("openXmlPart");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
Stream partStream = openXmlPart.GetStream(FileMode.Create);
this.Init(partStream, /*closeOutput*/true, encoding);
}
示例7: PutXmlDocument
public static void PutXmlDocument(OpenXmlPart part, XmlDocument xmlDoc)
{
using (Stream partStream = part.GetStream(FileMode.Create, FileAccess.Write))
using (XmlWriter partXmlWriter = XmlWriter.Create(partStream))
xmlDoc.Save(partXmlWriter);
}
示例8: SavePartAs
public static void SavePartAs(OpenXmlPart part, string filePath)
{
Stream partStream = part.GetStream(FileMode.Open, FileAccess.Read);
byte[] partContent = new byte[partStream.Length];
partStream.Read(partContent, 0, (int)partStream.Length);
File.WriteAllBytes(filePath, partContent);
}
示例9: WriteElementToCustomXmlPart
/// <summary>
/// Writes the element to custom XML part.
/// </summary>
/// <param name="customXmlPart">The custom XML part.</param>
/// <param name="rootElement">The root element.</param>
public static void WriteElementToCustomXmlPart(OpenXmlPart customXmlPart, XNode rootElement)
{
if (customXmlPart == null)
{
throw new ArgumentNullException("customXmlPart");
}
if (rootElement == null)
{
throw new ArgumentNullException("rootElement");
}
using (var writer = XmlWriter.Create(customXmlPart.GetStream(FileMode.Create, FileAccess.Write)))
{
rootElement.WriteTo(writer);
writer.Flush();
}
}
示例10: GetFirstElementFromCustomXmlPart
/// <summary>
/// Gets the first element from custom XML part.
/// </summary>
/// <param name="customXmlPart">The custom XML part.</param>
/// <param name="elementName">Name of the element.</param>
/// <returns>Returns the first element that matches the element name and the custom xml part</returns>
public XElement GetFirstElementFromCustomXmlPart(OpenXmlPart customXmlPart, string elementName)
{
if (customXmlPart == null)
{
throw new ArgumentNullException("customXmlPart");
}
if (string.IsNullOrEmpty(elementName))
{
throw new ArgumentNullException("elementName");
}
XDocument customPartDoc;
using (var reader = XmlReader.Create(customXmlPart.GetStream(FileMode.Open, FileAccess.Read)))
{
customPartDoc = XDocument.Load(reader);
}
var elementXName = XName.Get(elementName, this.NamespaceUri.ToString());
var element = (from e in customPartDoc.Descendants(elementXName)
select e).FirstOrDefault();
return element;
}
示例11: SaveToPart
/// <summary>
/// Save the DOM into the OpenXML part.
/// </summary>
internal void SaveToPart(OpenXmlPart openXmlPart)
{
if (openXmlPart == null)
{
throw new ArgumentNullException("openXmlPart");
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.CloseOutput = true;
// default is UTF8, Indent=false, OmitXmlDeclaration=false, NewLineOnAttributes=false
// settings.Indent = false;
// settings.Encoding = Encoding.UTF8;
// settings.OmitXmlDeclaration = false;
// settings.NewLineOnAttributes = false;
using (Stream partStream = openXmlPart.GetStream(FileMode.Create))
using (XmlWriter xmlWriter = XmlWriter.Create(partStream, settings))
{
if (this._standaloneDeclaration != null)
{
xmlWriter.WriteStartDocument(this._standaloneDeclaration.Value);
}
this.WriteTo(xmlWriter);
// fix bug #242463.
// Do not call WriteEndDocument if this root element is not parsed.
// In that case, the WriteTo() just call WriteRaw() with the raw xml, so no WriteStartElement() be called.
// So, the XmlWriter still on document start state. Call WriteEndDocument() will cause exception.
if (this.XmlParsed)
{
xmlWriter.WriteEndDocument();
}
}
}
示例12: LoadFromPart
/// <summary>
/// Load the DOM tree from the Open XML part.
/// </summary>
/// <param name="openXmlPart">The part this root element to be loaded from.</param>
/// <exception cref="InvalidDataException">Thrown when the part contains an incorrect root element.</exception>
internal void LoadFromPart(OpenXmlPart openXmlPart)
{
using (Stream partStream = openXmlPart.GetStream(FileMode.Open))
{
this.LoadFromPart(openXmlPart, partStream);
}
}
示例13: GenerateImagePart
private void GenerateImagePart(OpenXmlPart part, string imagePath, ref long imageWidthEMU, ref long imageHeightEMU)
{
byte[] imageFileBytes;
Bitmap imageFile;
using (FileStream fsImageFile = File.OpenRead(imagePath))
{
imageFileBytes = new byte[fsImageFile.Length];
fsImageFile.Read(imageFileBytes, 0, imageFileBytes.Length);
imageFile = new Bitmap(fsImageFile);
}
imageWidthEMU = (long)((imageFile.Width / imageFile.HorizontalResolution) * 914400L * 7);
imageHeightEMU = (long)((imageFile.Height / imageFile.VerticalResolution) * 914400L * 7);
using (BinaryWriter writer = new BinaryWriter(part.GetStream()))
{
writer.Write(imageFileBytes);
writer.Flush();
}
}
示例14: GetXmlDocument
public static XmlDocument GetXmlDocument(OpenXmlPart part)
{
XmlDocument xmlDoc = new XmlDocument();
using (Stream partStream = part.GetStream())
using (XmlReader partXmlReader = XmlReader.Create(partStream))
xmlDoc.Load(partXmlReader);
return xmlDoc;
}