/// <summary> /// Gets the XDocument for a part /// </summary> public static XDocument GetXDocument(this OpenXmlPart part) { XDocument xdoc = part.Annotation <XDocument>(); if (xdoc != null) { return(xdoc); } try { using (StreamReader sr = new StreamReader(part.GetStream())) using (XmlReader xr = XmlReader.Create(sr)) { xdoc = XDocument.Load(xr); xdoc.Changed += ElementChanged; xdoc.Changing += ElementChanged; } } catch (XmlException) { xdoc = new XDocument(); xdoc.AddAnnotation(new ChangedSemaphore()); } part.AddAnnotation(xdoc); return(xdoc); }
public void ValidateSave() { WorldEntity world = createTestWorld(); XElement xElement = WorldTransformer.Instance.ToXElement(world, TransformerSettings.WorldNamespace + "World"); xElement.Save("unittestsave.xml", SaveOptions.OmitDuplicateNamespaces); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.Add(null, "WorldSchema1.xsd"); XDocument xDocument = new XDocument(xElement); xDocument.AddAnnotation(SaveOptions.OmitDuplicateNamespaces); xDocument.Save("unittest2.xml", SaveOptions.OmitDuplicateNamespaces); XElement x1 = new XElement(TransformerSettings.WorldNamespace + "Outer"); x1.Add(new XElement(TransformerSettings.WorldNamespace + "Inner")); x1.Save("unittest3.xml", SaveOptions.OmitDuplicateNamespaces); string val = ""; xDocument.Validate(schemaSet, (o, vea) => { val += o.GetType().Name + "\n"; val += vea.Message + "\n"; }, true); Assert.AreEqual("", val); }
/// <summary> /// Configures the instance document. /// </summary> void Initialize(XDocument document) { if (document != null) { if (model != null) { document.AddAnnotation(model.Interface <Model>()); } if (instance != null) { document.AddAnnotation(instance.Interface <Instance>()); } document.Validate(new XmlSchemaSet(), (s, a) => { }, true); } this.document = document; }
private static void ElementChangedHandler(object sender, XObjectChangeEventArgs e) { XDocument xDocument = ((XObject)sender).Document; if (xDocument != null) { xDocument.Changing -= ElementChanged; xDocument.Changed -= ElementChanged; xDocument.AddAnnotation(new ChangedSemaphore()); } }
public void SetUp() { string dir = @"..\..\AddIns"; addInFiles = new List <XDocument>(); foreach (string file in Directory.GetFiles(dir, "*.addin", SearchOption.AllDirectories)) { XDocument doc = XDocument.Load(file); doc.AddAnnotation(FileName.Create(file)); addInFiles.Add(doc); } }
private static string GetDefaultParagraphStyleName(XDocument stylesXDoc) { XElement defaultParagraphStyle; string? defaultParagraphStyleName = null; var stylesInfo = stylesXDoc.Annotation <StylesInfo>(); if (stylesInfo != null) { defaultParagraphStyleName = stylesInfo.DefaultParagraphStyleName; } else { defaultParagraphStyle = stylesXDoc .Root .Elements(W.style) .FirstOrDefault(s => { if ((string)s.Attribute(W.type) != "paragraph") { return(false); } var defaultAttribute = s.Attribute(W._default); if (defaultAttribute != null && s.Attribute(W._default).ToBoolean().HasValue) { return(s.Attribute(W._default)?.ToBoolean() ?? false); } return(false); }); defaultParagraphStyleName = null; if (defaultParagraphStyle != null) { defaultParagraphStyleName = (string)defaultParagraphStyle.Attribute(W.styleId); } stylesInfo = new StylesInfo() { DefaultParagraphStyleName = defaultParagraphStyleName, }; stylesXDoc.AddAnnotation(stylesInfo); } return(defaultParagraphStyleName); }
private static void SerializeWithSaveOptions(SerializeNode serialize, bool testXElement, bool testXDocument) { // Test both options at once as they don't really collide SaveOptions so = SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces; XElement root = XElement.Parse("<root xmlns:a='uri'><child xmlns:a='uri'><baby xmlns:a='uri'>text</baby></child></root>"); XElement child = root.Element("child"); XElement baby = child.Element("baby"); XNode text = baby.FirstNode; // Verify that without annotation the output gets indented and the duplicate ns decls are not removed if (testXElement) { Assert.Equal("<child xmlns:a=\"uri\"> <baby xmlns:a=\"uri\">text</baby></child>", NormalizeNewLines(serialize(child))); } // Now add annotation to the leaf element node // Even though it's in effect the output should stay the same (as there is only one namespace decl and mixed content). baby.AddAnnotation(so); if (testXElement) { Assert.Equal("<baby xmlns:a=\"uri\">text</baby>", serialize(baby)); } // Now add annotation to the middle node child.AddAnnotation(so); if (testXElement) { // Verify that the options are applied correctly Assert.Equal("<child xmlns:a=\"uri\"><baby>text</baby></child>", NormalizeNewLines(serialize(child))); // Verify that the root node is not affected as we don't look for the annotation among descendants Assert.Equal("<root xmlns:a=\"uri\"> <child xmlns:a=\"uri\"> <baby xmlns:a=\"uri\">text</baby> </child></root>", NormalizeNewLines(serialize(root))); } // And now add the annotation to the root and remove it from the child to test that we can correctly skip over a node root.AddAnnotation(so); child.RemoveAnnotations(typeof(SaveOptions)); if (testXElement) { // Verify that the options are still applied to child Assert.Equal("<child xmlns:a=\"uri\"><baby>text</baby></child>", serialize(child)); // And they should be also applied to the root now Assert.Equal("<root xmlns:a=\"uri\"><child><baby>text</baby></child></root>", serialize(root)); } // Add a document node above it all to test that it works on non-XElement as well XDocument doc = new XDocument(root); // Add the annotation to the doc and remove it from the root doc.AddAnnotation(so); root.RemoveAnnotations(typeof(SaveOptions)); // Options should still apply to root as well as the doc if (testXElement) { Assert.Equal("<root xmlns:a=\"uri\"><child><baby>text</baby></child></root>", serialize(root)); } if (testXDocument) { Assert.Equal("<root xmlns:a=\"uri\"><child><baby>text</baby></child></root>", serialize(doc)); } }
// ReSharper disable once SuggestBaseTypeForParameter private static void MarkAsUsesFullTypeName(XDocument xmlAnnotationDoc) => xmlAnnotationDoc.AddAnnotation(UseFullTypeNameAnnotation.Instance);
protected override void Arrange() { _invalidCompositeDefinition.AddAnnotation("PathToCompositeDefinition"); _compositeMetadataValidator = new CompositeMetadataValidator(); }