public void get_report_list() { var username = ""; var password = ""; var ub = new UriBuilder("https", "reports.office365.com"); ub.Path = "ecp/reportingwebservice/reporting.svc"; var fullRestURL = Uri.EscapeUriString(ub.Uri.ToString()); var request = (HttpWebRequest)HttpWebRequest.Create(fullRestURL); request.Credentials = new NetworkCredential(username, password); try { var response = (HttpWebResponse)request.GetResponse(); var encode = System.Text.Encoding.GetEncoding("utf-8"); var readStream = new StreamReader(response.GetResponseStream(), encode); var doc = new XDocument(); doc = XDocument.Load(response.GetResponseStream()); var nodes = doc.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Text).ToList(); var node_str = string.Join(",", nodes); doc.Save(@"C:\Office365\Reports\ReportList.xml"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Wrap an existing <see cref="XDocument"/> as an <see cref="XdmNode"/>. This is the <see cref="XDocument"/> /// equivalent of <see cref="DocumentBuilder.Wrap(XmlDocument)"/>. /// </summary> /// <remarks> /// PoC: /// /// Creates wrapper objects for all nodes in the document graph and stores them using /// <see cref="XObject.AddAnnotation(object)"/>. Will throw if any node has already been wrapped. /// /// Idealy this would be an extension to <see cref="DocumentBuilder"/>, but DocumentBuilder does not expose /// its Configuration object publically. /// </remarks> public static XdmNode Wrap(this Processor processor, XDocument doc) { if (doc.Annotation<XObjectWrapper>() != null) throw new InvalidOperationException("XDocument is already annotated with a wrapper."); var docWrapper = (XDocumentWrapper)XObjectWrapper.MakeWrapper(doc); docWrapper.setConfiguration(processor.Implementation); doc.AddAnnotation(docWrapper); foreach (var node in doc.DescendantNodes()) { if (node.Annotation<XObjectWrapper>() != null) throw new InvalidOperationException(string.Format("{0} is already annotated with a wrapper.", node.GetType().Name)); node.AddAnnotation(XObjectWrapper.MakeWrapper(node)); if (node.NodeType == XmlNodeType.Element) { foreach (var attr in ((XElement)node).Attributes()) { if (attr.Annotation<XObjectWrapper>() != null) throw new InvalidOperationException("Attribute is already annotated with a wrapper."); attr.AddAnnotation(XObjectWrapper.MakeWrapper(attr)); } } } return (XdmNode)XdmValue.Wrap(docWrapper); }
private void RemoveComments(XDocument document) { var comments = document.DescendantNodes().OfType<XComment>().ToArray(); foreach(var comment in comments) { comment.Remove(); } }
public XDocument Modify(XDocument document) { var textNodes = document.DescendantNodes() .OfType<XText>() .Where(ShouldNormalize); foreach (var textNode in textNodes) { textNode.Value = NormalizeWhitespace(textNode.Value); } return document; }
static void ForceEmptyTagsWithNewlines(XDocument document) { // this is to force compatibility with previous implementation, // in particular, to support nested objects with null properties in them. foreach (var childElement in from x in document.DescendantNodes().OfType<XElement>() where x.IsEmpty && !x.HasAttributes select x) { childElement.Value = "\n"; } }
//function for get tags from parsed DICOM public static String GetElement(System.Xml.Linq.XDocument doc, string elementName) { foreach (XNode node in doc.DescendantNodes()) { if (node is XElement) { XElement element = (XElement)node; if (element.Name.LocalName.Equals("DataElement")) { if (element.Attribute("Tag").Value == elementName) { return(element.Attribute("Data").Value); } } } } return(null); }
private static XDocument FormatCodeSamples(XDocument doc, IEnumerable<IParser> parsers) { var xElements = doc.DescendantNodes().OfType<XElement>().ToList(); var codeSamples = xElements.Where( node => node!=null && node.Attributes().Any(a => a.Name == "class" && a.Value == "csharpcode")); foreach (var codeSample in codeSamples) { var encodedCSharp = codeSample.AsText(); var code = System.Net.WebUtility.HtmlDecode(encodedCSharp); var formattedCode = Format(code, parsers); var newContent = formattedCode.ParseXml(); codeSample.ReplaceWith(newContent); } return doc; }
public int RemoveFavorite(string uniqueIdentity, string type) { try { System.Xml.Linq.XDocument resultsDoc = System.Xml.Linq.XDocument.Parse((System.Xml.Linq.XDocument.Load(Datacontext.GetInstance().CorporateFavoriteFile)).ToString()); var query = resultsDoc.Root.Descendants("Favorites"); foreach (System.Xml.Linq.XElement e in query) { if (e.Element("UniqueIdentity").Value.ToString() == uniqueIdentity && e.Element("Type").Value.ToString() == type) { var collection = resultsDoc.DescendantNodes(); e.Remove(); resultsDoc.Save(Datacontext.GetInstance().CorporateFavoriteFile); return(1); } } } catch (Exception ex) { _logger.Error("Error in creating removing XMLFile : " + ex.Message.ToString()); } return(0); }
public void Process() { ExtractCommandsAndResults(); // Sort server commands and responses .. XDocument = new XDocument(new XElement("Log")); var root = XDocument.Root; if (root != null) for (var i = 0; i < Commands.Count; i++) { root.Add(XElement.Parse(XmlUtility.SerializeArrayToXml(Commands[i]))); root.Add(XElement.Parse(XmlUtility.SerializeToXml(Results[i]))); } // Remove comments .. XDocument.DescendantNodes().Where(node => node.NodeType == XmlNodeType.Comment).Remove(); // Convert (.doc) reports to txt .. foreach (var reportData in XDocument.Descendants() .Where(node => node.Name.LocalName == typeof(ServerCommandResult).Name) .Where(cmdRes => cmdRes.Descendants().Any(desc => desc.Name.LocalName == "Message" && desc.Value == "Report generated")) .SelectMany(cmdResRep => cmdResRep.Descendants()).Where(desc => desc.Name.LocalName == "Data")) { reportData.Value = ConvertDocx(reportData.Value); } // Remove DslScript .. ProcessRemoveDslScript(XDocument); }
void ExpandPlugIns(ViewContext viewContext, XDocument html, List<XElement> scripts) { var pis = html.DescendantNodes().OfType<XProcessingInstruction>().ToArray(); ExpandPlugins(viewContext, pis, html.Root.Element("head"), scripts); }
void DropNSFromDocument (XDocument doc) { var attributes = doc.Descendants ().SelectMany (n => n.Attributes ()); var textNodes = doc.DescendantNodes().OfType<XText> ().ToArray(); DropNS (attributes, textNodes); }
using System;
internal void PrepareDocument(XDocument document) { /* Remove all HTML comments (including conditional comments). */ document .DescendantNodes() .Where(node => node.NodeType == XmlNodeType.Comment) .Remove(); /* In some cases a body element can't be found (if the HTML is totally hosed for example), * so we create a new body element and append it to the document. */ XElement documentBody = GetOrCreateBody(document); XElement rootElement = document.Root; // TODO: handle HTML frames var elementsToRemove = new List<XElement>(); /* Remove all scripts that are not readability. */ elementsToRemove.Clear(); rootElement.GetElementsByTagName("script") .ForEach(scriptElement => { string scriptSrc = scriptElement.GetAttributeValue("src", null); if (string.IsNullOrEmpty(scriptSrc) || scriptSrc.LastIndexOf("readability") == -1) { elementsToRemove.Add(scriptElement); } }); RemoveElements(elementsToRemove); /* Remove all noscript tags. */ elementsToRemove.Clear(); elementsToRemove.AddRange(rootElement.GetElementsByTagName("noscript")); RemoveElements(elementsToRemove); /* Remove all external stylesheets. */ elementsToRemove.Clear(); elementsToRemove.AddRange( rootElement.GetElementsByTagName("link") .Where(element => element.GetAttributeValue("rel", "").Trim().ToLower() == "stylesheet" && element.GetAttributeValue("href", "").LastIndexOf("readability") == -1)); RemoveElements(elementsToRemove); /* Remove all style tags. */ elementsToRemove.Clear(); elementsToRemove.AddRange(rootElement.GetElementsByTagName("style")); RemoveElements(elementsToRemove); /* Remove all nav tags. */ elementsToRemove.Clear(); elementsToRemove.AddRange(rootElement.GetElementsByTagName("nav")); RemoveElements(elementsToRemove); /* Remove all anchors. */ elementsToRemove.Clear(); IEnumerable<XElement> anchorElements = rootElement.GetElementsByTagName("a") .Where(aElement => aElement.Attribute("name") != null && aElement.Attribute("href") == null); elementsToRemove.AddRange(anchorElements); RemoveElements(elementsToRemove); /* Turn all double br's into p's and all font's into span's. */ // TODO: optimize? string bodyInnerHtml = documentBody.GetInnerHtml(); bodyInnerHtml = _ReplaceDoubleBrsRegex.Replace(bodyInnerHtml, "</p><p>"); bodyInnerHtml = _ReplaceFontsRegex.Replace(bodyInnerHtml, "<$1span>"); documentBody.SetInnerHtml(bodyInnerHtml); }
// __ Impl _______________________________________________________ private static ObjectData GetObjectFromXml(XDocument xmlDocument) { ObjectData currentObject = null; FieldData currentField = null; if (xmlDocument.DescendantNodes().Any()) { currentObject = new ObjectData(); IEnumerable<XNode> nodes = xmlDocument.DescendantNodes(); foreach (dynamic node in nodes) { if (node is XElement) { string name = node.Name.LocalName; var xnode = node as XElement; switch (name) { case "object": var nameElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "name"); if (nameElement != null) currentObject.Name = nameElement.Value; var settigsElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "settings"); if (settigsElement != null) currentObject.IsSettingsInt = GetIntFromBoolString(settigsElement.Value); currentObject.IsSingleInstInt = GetIntFromBoolString( xnode.Attributes() .FirstOrDefault(a => a.Name.LocalName == "singleinstance") .Value); break; case "description": currentObject.Description = node.Value; break; case "field": currentField = new FieldData(); currentField.Name = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "name").Value; currentObject.FieldsIndexedByName.Add(currentField.Name, currentField); if (IsClone(node)) { currentField.CloneFrom( currentObject.FieldsIndexedByName[ xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "cloneof").Value]); } else { XAttribute typeElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "type"); if (typeElement != null) { string typeString = typeElement.Value; currentField.TypeString = typeString; currentField.Type = GetFieldTypeFromString(currentField.TypeString); XAttribute elementsAttribute = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "elements"); if (elementsAttribute != null) currentField.Elements = elementsAttribute.Value; XAttribute unitsElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "units"); if (unitsElement != null) currentField.Units = unitsElement.Value; XAttribute elementNamesElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "elementnames"); if ( elementNamesElement != null) currentField.ParseElementNamesFromAttribute(elementNamesElement.Value); XAttribute optionsElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "options"); if (optionsElement != null) currentField.ParseOptionsFromAttribute(optionsElement.Value); XAttribute defaultValueElement = xnode.Attributes().FirstOrDefault(a => a.Name.LocalName == "defaultvalue"); if ( defaultValueElement != null) currentField.ParseDefaultValuesFromAttribute(defaultValueElement.Value); } } currentObject.Fields.Add(currentField); break; case "option": currentField.Options.Add(node.Value); break; case "elementname": currentField.ElementNames.Add(node.Value); break; } } } ExpandDefaultValues(currentObject); SortFields(currentObject); SummaryGenerator.RegisterObjectId( Hasher.CalculateId(currentObject), string.Format("{0}.{1}", CSharpGenerator.Namespace, currentObject.Name)); } return currentObject; }
//=========================================================================================== private void RemoveComments() { _Document = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\..\Xsd\MagickScript.xsd"); _Document.DescendantNodes().OfType<XComment>().Remove(); }
/// <summary> /// Shuffle elements values of a XML document. /// </summary> /// <param name="xmlDocument">A XElement object for a shuffle.</param> /// <param name="shuffleOnlyVowels">Specifies when only vowels shuffle is needed.</param> /// <param name="separators">User defined words separators in xml elements values.</param> public static void ShuffleXmlElementsValues( XDocument xmlDocument, Boolean shuffleOnlyVowels, params Char[] separators ) { var textNodes = from node in xmlDocument.DescendantNodes() where node is XText select (XText)node; foreach (var textNode in textNodes) { string tempValue = textNode.Value; tempValue = ShuffleStatement(tempValue, shuffleOnlyVowels, separators); textNode.Value = tempValue; } }
/// <summary> /// Creates the document. /// </summary> /// <param name="mergeTemplate">The merge template.</param> /// <param name="mergeObjectList">The merge object list.</param> /// <param name="globalMergeFields">The global merge fields.</param> /// <returns></returns> public override BinaryFile CreateDocument( MergeTemplate mergeTemplate, List<object> mergeObjectList, Dictionary<string, object> globalMergeFields ) { this.Exceptions = new List<Exception>(); BinaryFile outputBinaryFile = null; var rockContext = new RockContext(); var binaryFileService = new BinaryFileService( rockContext ); var templateBinaryFile = binaryFileService.Get( mergeTemplate.TemplateBinaryFileId ); if ( templateBinaryFile == null ) { return null; } // Start by creating a new document with the contents of the Template (so that Styles, etc get included) XDocument sourceTemplateDocX; using ( MemoryStream outputDocStream = new MemoryStream() ) { templateBinaryFile.ContentStream.CopyTo( outputDocStream ); outputDocStream.Seek( 0, SeekOrigin.Begin ); // now that we have the outputdoc started, simplify the sourceTemplate var sourceTemplateStream = templateBinaryFile.ContentStream; var simplifiedDoc = WordprocessingDocument.Open( sourceTemplateStream, true ); MarkupSimplifier.SimplifyMarkup( simplifiedDoc, this.simplifyMarkupSettingsAll ); //// simplify any nodes that have Lava in it that might not have been caught by the MarkupSimplifier //// MarkupSimplifier only merges superfluous runs that are children of a paragraph sourceTemplateDocX = simplifiedDoc.MainDocumentPart.GetXDocument(); OpenXmlRegex.Match( sourceTemplateDocX.Elements(), this.lavaRegEx, ( x, m ) => { foreach ( var nonParagraphRunsParent in x.DescendantNodes().OfType<XElement>().Where( a => a.Parent != null && a.Name != null ) .Where( a => ( a.Name.LocalName == "r" ) ).Select( a => a.Parent ).Distinct().ToList() ) { if ( lavaRegEx.IsMatch( nonParagraphRunsParent.Value ) ) { var tempParent = XElement.Parse( new Paragraph().OuterXml ); tempParent.Add( nonParagraphRunsParent.Nodes() ); tempParent = MarkupSimplifier.MergeAdjacentSuperfluousRuns( tempParent ); nonParagraphRunsParent.ReplaceNodes( tempParent.Nodes() ); } } } ); XElement lastLavaNode = sourceTemplateDocX.DescendantNodes().OfType<XElement>().LastOrDefault( a => lavaRegEx.IsMatch( a.Value ) ); // ensure there is a { Next } indicator after the last lava node in the template if (lastLavaNode != null) { var nextRecordMatch = nextRecordRegEx.Match( lastLavaNode.Value ); if ( nextRecordMatch == null || !nextRecordMatch.Success ) { // if the last lava node doesn't have a { next }, append to the end lastLavaNode.Value += " {% next %} "; } else { if ( !lastLavaNode.Value.EndsWith( nextRecordMatch.Value ) ) { // if the last lava node does have a { next }, but there is stuff after it, add it (just in case) lastLavaNode.Value += " {% next %} "; } } } bool? allSameParent = null; using ( WordprocessingDocument outputDoc = WordprocessingDocument.Open( outputDocStream, true ) ) { var xdoc = outputDoc.MainDocumentPart.GetXDocument(); var outputBodyNode = xdoc.DescendantNodes().OfType<XElement>().FirstOrDefault( a => a.Name.LocalName.Equals( "body" ) ); outputBodyNode.RemoveNodes(); int recordIndex = 0; int? lastRecordIndex = null; int recordCount = mergeObjectList.Count(); while ( recordIndex < recordCount ) { if ( lastRecordIndex.HasValue && lastRecordIndex == recordIndex ) { // something went wrong, so throw to avoid spinning infinitely throw new Exception( "Unexpected unchanged recordIndex" ); } lastRecordIndex = recordIndex; using ( var tempMergeTemplateStream = new MemoryStream() ) { sourceTemplateStream.Position = 0; sourceTemplateStream.CopyTo( tempMergeTemplateStream ); tempMergeTemplateStream.Position = 0; var tempMergeTemplateX = new XDocument( sourceTemplateDocX ); var tempMergeTemplateBodyNode = tempMergeTemplateX.DescendantNodes().OfType<XElement>().FirstOrDefault( a => a.Name.LocalName.Equals( "body" ) ); // find all the Nodes that have a {% next %}. List<XElement> nextIndicatorNodes = new List<XElement>(); OpenXmlRegex.Match( tempMergeTemplateX.Elements(), this.nextRecordRegEx, ( x, m ) => { nextIndicatorNodes.Add( x ); } ); allSameParent = allSameParent ?? nextIndicatorNodes.Count > 1 && nextIndicatorNodes.Select( a => a.Parent ).Distinct().Count() == 1; List<XContainer> recordContainerNodes = new List<XContainer>(); foreach ( var nextIndicatorNodeParent in nextIndicatorNodes.Select( a => a.Parent ).Where( a => a != null ) ) { XContainer recordContainerNode = nextIndicatorNodeParent; if ( !allSameParent.Value ) { // go up the parent nodes until we have more than one "Next" descendent so that we know what to consider our record container while ( recordContainerNode.Parent != null ) { if ( this.nextRecordRegEx.Matches( recordContainerNode.Parent.Value ).Count == 1 ) { // still just the one "next" indicator, so go out another parent recordContainerNode = recordContainerNode.Parent; } else { // we went too far up the parents and found multiple "next" children, so use this node as the recordContainerNode break; } } } if ( !recordContainerNodes.Contains( recordContainerNode ) ) { recordContainerNodes.Add( recordContainerNode ); } } foreach ( var recordContainerNode in recordContainerNodes ) { //// loop thru each of the recordContainerNodes //// If we have more records than nodes, we'll jump out to the outer "while" and append another template and keep going XContainer mergedXRecord; var recordContainerNodeXml = recordContainerNode.ToString( SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces ).ReplaceWordChars(); if ( recordIndex >= recordCount ) { // out of records, so clear out any remaining template nodes that haven't been merged string xml = recordContainerNodeXml; mergedXRecord = XElement.Parse( xml ) as XContainer; OpenXmlRegex.Replace( mergedXRecord.Nodes().OfType<XElement>(), this.regExDot, string.Empty, ( a, b ) => { return true; } ); recordIndex++; } else { //// just in case they have shared parent node, or if there is trailing {{ next }} after the last lava //// on the page, split the XML for each record and reassemble it when done List<string> xmlChunks = this.nextRecordRegEx.Split( recordContainerNodeXml ).ToList(); string mergedXml = string.Empty; foreach ( var xml in xmlChunks ) { bool incRecordIndex = true; if ( lavaRegEx.IsMatch(xml) ) { if ( recordIndex < recordCount ) { try { DotLiquid.Hash wordMergeObjects = new DotLiquid.Hash(); wordMergeObjects.Add( "Row", mergeObjectList[recordIndex] ); foreach ( var field in globalMergeFields ) { wordMergeObjects.Add( field.Key, field.Value ); } var resolvedXml = xml.ResolveMergeFields( wordMergeObjects, true, true ); mergedXml += resolvedXml; if (resolvedXml == xml) { // there weren't any MergeFields after all, so don't move to the next record incRecordIndex = false; } } catch ( Exception ex ) { // if ResolveMergeFields failed, log the exception, then just return the orig xml this.Exceptions.Add( ex ); mergedXml += xml; } if ( incRecordIndex ) { recordIndex++; } } else { // out of records, so put a special '{% next_empty %}' that we can use to clear up unmerged parts of the template mergedXml += " {% next_empty %} " + xml; } } else { mergedXml += xml; } } mergedXRecord = XElement.Parse( mergedXml ) as XContainer; } // remove the orig nodes and replace with merged nodes recordContainerNode.RemoveNodes(); recordContainerNode.Add( mergedXRecord.Nodes().OfType<XElement>() ); var mergedRecordContainer = XElement.Parse( recordContainerNode.ToString( SaveOptions.DisableFormatting ) ); if ( recordContainerNode.Parent != null ) { // the recordContainerNode is some child/descendent of <body> recordContainerNode.ReplaceWith( mergedRecordContainer ); } else { // the recordContainerNode is the <body> recordContainerNode.RemoveNodes(); recordContainerNode.Add( mergedRecordContainer.Nodes() ); if ( recordIndex < recordCount ) { // add page break var pageBreakXml = new Paragraph( new Run( new Break() { Type = BreakValues.Page } ) ).OuterXml; var pageBreak = XElement.Parse( pageBreakXml, LoadOptions.None ); var lastParagraph = recordContainerNode.Nodes().OfType<XElement>().Where( a => a.Name.LocalName == "p" ).LastOrDefault(); if ( lastParagraph != null ) { lastParagraph.AddAfterSelf( pageBreak ); } } } } outputBodyNode.Add( tempMergeTemplateBodyNode.Nodes() ); } } // remove all the 'next' delimiters OpenXmlRegex.Replace( outputBodyNode.Nodes().OfType<XElement>(), this.nextRecordRegEx, string.Empty, ( xx, mm ) => { return true; } ); // find all the 'next_empty' delimiters that we might have added and clear out the content in the paragraph nodes that follow OpenXmlRegex.Match( outputBodyNode.Nodes().OfType<XElement>(), this.nextEmptyRecordRegEx, ( xx, mm ) => { var afterSiblings = xx.ElementsAfterSelf().ToList(); // get all the paragraph elements after the 'next_empty' node and clear out the content var nodesToClean = afterSiblings.Where( a => a.Name.LocalName == "p" ).ToList(); // if the next_empty node has lava, clean that up too var xxContent = xx.ToString(); if ( lavaRegEx.IsMatch(xxContent) ) { nodesToClean.Add( xx ); } foreach (var node in nodesToClean) { // remove all child nodes from each paragraph node if (node.HasElements) { node.RemoveNodes(); } } } ); // remove all the 'next_empty' delimiters OpenXmlRegex.Replace( outputBodyNode.Nodes().OfType<XElement>(), this.nextEmptyRecordRegEx, string.Empty, ( xx, mm ) => { return true; } ); // remove all but the last SectionProperties element (there should only be one per section (body)) var sectPrItems = outputBodyNode.Nodes().OfType<XElement>().Where( a => a.Name.LocalName == "sectPr" ); foreach ( var extra in sectPrItems.Where( a => a != sectPrItems.Last() ).ToList() ) { extra.Remove(); } // renumber all the ids to make sure they are unique var idAttrs = xdoc.DescendantNodes().OfType<XElement>().Where( a => a.HasAttributes ).Select( a => a.Attribute( "id" ) ).Where( s => s != null ); int lastId = 1; foreach ( var attr in idAttrs ) { attr.Value = lastId.ToString(); lastId++; } DotLiquid.Hash globalMergeHash = new DotLiquid.Hash(); foreach ( var field in globalMergeFields ) { globalMergeHash.Add( field.Key, field.Value ); } HeaderFooterGlobalMerge( outputDoc, globalMergeHash ); // sweep thru any remaining un-merged body parts for any Lava having to do with Global merge fields foreach ( var bodyTextPart in outputDoc.MainDocumentPart.Document.Body.Descendants<Text>() ) { string nodeText = bodyTextPart.Text.ReplaceWordChars(); if ( lavaRegEx.IsMatch( nodeText ) ) { bodyTextPart.Text = nodeText.ResolveMergeFields( globalMergeHash, true, true ); } } // remove the last pagebreak MarkupSimplifier.SimplifyMarkup( outputDoc, new SimplifyMarkupSettings { RemoveLastRenderedPageBreak = true } ); // If you want to see validation errors /* var validator = new OpenXmlValidator(); var errors = validator.Validate( outputDoc ).ToList(); */ } outputBinaryFile = new BinaryFile(); outputBinaryFile.IsTemporary = true; outputBinaryFile.ContentStream = outputDocStream; outputBinaryFile.FileName = "MergeTemplateOutput" + Path.GetExtension( templateBinaryFile.FileName ); outputBinaryFile.MimeType = templateBinaryFile.MimeType; outputBinaryFile.BinaryFileTypeId = new BinaryFileTypeService( rockContext ).Get( Rock.SystemGuid.BinaryFiletype.DEFAULT.AsGuid() ).Id; binaryFileService.Add( outputBinaryFile ); rockContext.SaveChanges(); } return outputBinaryFile; }
/// <summary> /// Insert line break elements where needed to preserve text formatting /// </summary> /// <param name="document">The document in which to insert line breaks</param> private static void InsertLineBreaks(XDocument document) { XElement element, lastElement = null; string[] parts; string part; bool skipBreak; foreach(var splitText in document.DescendantNodes().OfType<XElement>().Where( t => t.Name.LocalName == "t" && (string)t.Attribute(XNamespace.Xml + "space") == "preserve" && reLineBreaks.IsMatch(t.Value)).ToList()) { // For single line breaks, use an empty string so that we don't gain an extra line break if(splitText.Value == "\r\n" || splitText.Value == "\r" || splitText.Value == "\n") splitText.Value = String.Empty; parts = reLineBreaks.Split(splitText.Value); skipBreak = false; for(int idx = 0; idx < parts.Length; idx++) { part = parts[idx]; if(idx == 0) { // The source text element gets the first part or is replaced with a line break if empty if(part.Length != 0) { splitText.Value = part; lastElement = splitText; } else { lastElement = new XElement(w + "br"); splitText.ReplaceWith(lastElement); skipBreak = true; // Prevent an extra line break from being added after this one } } else { if(!skipBreak) { element = new XElement(w + "br"); lastElement.AddAfterSelf(element); lastElement = element; } else skipBreak = false; if(part.Length != 0) { element = new XElement(w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), part); lastElement.AddAfterSelf(element); lastElement = element; } } } } }
/// <summary> /// Wrap stray text nodes in text elements and, when necessary, run elements /// </summary> /// <param name="document">The document in which to wrap stray text nodes</param> /// <remarks>Stray text nodes can occur when resolving shared content items. We need to ensure that /// all text nodes are within a text element within a run.</remarks> private static void WrapStrayElementNodes(XDocument document) { XElement wrap; foreach(var text in document.DescendantNodes().OfType<XText>().Where( t => t.Parent.Name.LocalName != "t" && t.Parent.Name.LocalName != "a" && t.Parent.Name.LocalName != "span").ToList()) { #if DEBUG if(text.Parent.Name.LocalName != "body" && text.Parent.Name.LocalName != "p" && text.Parent.Name.LocalName != "tc") System.Diagnostics.Debug.WriteLine("Stray text parent: " + text.Parent.Name.LocalName); #endif if(text.Parent.Name.LocalName == "r") wrap = new XElement(w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), text.Value); else wrap = new XElement(w + "r", new XElement(w + "t", new XAttribute(XNamespace.Xml + "space", "preserve"), text.Value)); text.ReplaceWith(wrap); } }
private static void ShowAllComments(XDocument doc) { Console.WriteLine("Comments:"); foreach (var item in doc.DescendantNodes().OfType<XComment>()) { Console.WriteLine(item.ToString()); } Console.WriteLine("End of Comments"); }
//===================================================================== /// <summary> /// This applies the changes needed to convert the XML to a markdown topic file /// </summary> /// <param name="topic">The topic to which the changes are applied</param> /// <returns>The page title if one could be found</returns> private static string ApplyChanges(XDocument topic) { string topicTitle = null; var root = topic.Root; // Remove the filename element from API topics var filename = root.Element("file"); if(filename != null) filename.Remove(); foreach(var span in topic.Descendants("span").Where(s => s.Attribute("class") != null).ToList()) { string spanClass = span.Attribute("class").Value; switch(spanClass) { case "languageSpecificText": // Replace language-specific text with the neutral text sub-entry. If not found, // remove it. var genericText = span.Elements("span").FirstOrDefault( s => (string)s.Attribute("class") == "nu"); if(genericText != null) span.ReplaceWith(genericText.Value); else span.Remove(); break; default: // All other formatting spans are removed by moving the content up to the parent element. // The children of the LST spans are ignored since we've already handled them. if(span.Parent.Name == "span" && (string)span.Parent.Attribute("class") == "languageSpecificText") break; foreach(var child in span.Nodes().ToList()) { child.Remove(); span.AddBeforeSelf(child); } span.Remove(); break; } } var linkTargets = new Dictionary<string, string>(); // Remove link ID spans and change any links to them to use the page/section title instead. Note // that cross-page anchor references (PageName#Anchor) won't work and I'm not going to attempt to // support them since it would be more complicated. Likewise, links to elements without a title // such as list items and table cells won't work either. foreach(var span in topic.Descendants("span").Where(s => s.Attribute("id") != null).ToList()) { string id = span.Attribute("id").Value; var sectionTitle = span.PreviousNode as XText; if(sectionTitle != null) { // We may get more than one line so find the last one with a section title which will be // the closest to the span. string title = sectionTitle.Value.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Reverse().FirstOrDefault( t => t.Trim().Length > 2 && t[0] == '#'); if(title != null) { int pos = title.IndexOf(' '); if(pos != -1) { title = title.Substring(pos + 1).Trim(); // Extract the topic title for the sidebar TOC if(id == "PageHeader") topicTitle = title; // Convert the title ID to the expected format title = title.ToLowerInvariant().Replace(' ', '-').Replace("#", String.Empty); // For intro links, link to the page header title since intro sections have no title // themselves. The transformations always add a PageHeader link span after the page // title (or should). if(id.StartsWith("@pageHeader_", StringComparison.Ordinal)) linkTargets.Add(id.Substring(12), "PageHeader"); else linkTargets.Add(id, "#" + title); } } } span.Remove(); } // Update in-page link targets foreach(var anchor in topic.Descendants("a").ToList()) { var href = anchor.Attribute("href"); if(href.Value.Length > 1 && href.Value[0] == '#') { string id = href.Value.Substring(1).Trim(), target; if(linkTargets.TryGetValue(id, out target)) { if(target == "PageHeader") if(!linkTargets.TryGetValue("PageHeader", out target)) target = "#"; } else target = "#"; href.Value = target; } } // If we couldn't find a topic title, try to get the first section header title. It's probably a // user-added file. if(topicTitle == null) { var textBlock = topic.DescendantNodes().OfType<XText>().FirstOrDefault(); if(textBlock != null) { string title = textBlock.Value.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(t => t.Trim().Length > 2 && t[0] == '#'); if(title != null) { int pos = title.IndexOf(' '); if(pos != -1) topicTitle = title.Substring(pos + 1).Trim(); } } } return topicTitle; }
internal void SubstitutePlaceHolders(XDocument doc, DistributedConfigurationSubstitutions configurationSubstitutions) { var descendantComments = doc.DescendantNodes().OfType<XComment>(); var comments = (from comment in descendantComments let match = TypeIdentifier.Match(comment.Value) where match.Success select new { XComment = comment, Match = match }).ToList(); foreach (var comment in comments) { if (!comment.Match.Groups["selector"].Value.Equals("Replace")) continue; var parameters = new JavaScriptSerializer().DeserializeObject(comment.Match.Groups["object"].Value) as Dictionary<string, object>; var xPath = parameters["XPath"] as string; var value = parameters["Value"] as string; var node = (comment.XComment.XPathEvaluate(xPath) as IEnumerable<object>).FirstOrDefault(); if (node is XElement) ((XElement)node).SetValue(value); else if (node is XAttribute) ((XAttribute)node).SetValue(value); comment.XComment.Remove(); } var xmlAsString = doc.ToString(); // not efficient but clear and ok for usually small configs xmlAsString = Variable.Replace(xmlAsString, match => { var variableName = match.Groups["varName"].Value; var variable = configurationSubstitutions.Variables .FirstOrDefault(v => v.Name.Equals(variableName)); if (variable == null) return match.Value; return variable.Value; }); xmlAsString = Escaping.Replace(xmlAsString, string.Empty); var doc2 = XDocument.Parse(xmlAsString); doc.ReplaceNodes(doc2.Nodes()); }
internal void PrepareDocument(XDocument document) { /* Remove all HTML comments (including conditional comments). */ document .DescendantNodes() .Where(node => node.NodeType == XmlNodeType.Comment || (node.NodeType == XmlNodeType.Element && "script" == GetElementName((XElement) node))) .Remove(); /* In some cases a body element can't be found (if the HTML is totally hosed for example), * so we create a new body element and append it to the document. */ XElement documentBody = GetOrCreateBody(document); XElement rootElement = document.Root; // TODO: handle HTML frames var elementsToRemove = new List<XElement>(); /* Remove all noscript tags. */ elementsToRemove.Clear(); elementsToRemove.AddRange(rootElement.GetElementsByTagName("noscript")); RemoveElements(elementsToRemove); /* Remove all external stylesheets. */ elementsToRemove.Clear(); elementsToRemove.AddRange( rootElement.GetElementsByTagName("link")); RemoveElements(elementsToRemove); /* Remove all style tags. */ elementsToRemove.Clear(); elementsToRemove.AddRange(rootElement.GetElementsByTagName("style")); RemoveElements(elementsToRemove); /* Remove all nav tags. */ elementsToRemove.Clear(); elementsToRemove.AddRange(rootElement.GetElementsByTagName("nav")); RemoveElements(elementsToRemove); /* Remove all anchors. */ elementsToRemove.Clear(); IEnumerable<XElement> anchorElements = rootElement.GetElementsByTagName("a") .Where(aElement => aElement.Attribute("name") != null && aElement.Attribute("href") == null); elementsToRemove.AddRange(anchorElements); RemoveElements(elementsToRemove); /* Turn all double br's into p's and all font's into span's. */ // TODO: optimize? /*不用做这个,最后 格式化的过程中会去掉多余的换行,同时会把所有的font等同普通文本 string bodyInnerHtml = documentBody.GetInnerHtml(); bodyInnerHtml = _ReplaceDoubleBrsRegex.Replace(bodyInnerHtml, "</p><p>"); bodyInnerHtml = _ReplaceFontsRegex.Replace(bodyInnerHtml, "<$1span>"); documentBody.SetInnerHtml(bodyInnerHtml); * */ }