/// <summary> /// Stores the element in an extension. /// </summary> /// <param name="extensible">The object that is being extended.</param> /// <param name="element">The element to store.</param> /// <returns>This method always returns true.</returns> public bool StoreElement(IExtensible extensible, ElementInfo element) { TestExtension extension; extension = extensible.Extensions.FirstOrDefault((e) => e.Name == TestExtension.ExtensionName) as TestExtension; if (extension == null) { extension = new TestExtension(); extensible.Extensions.Add(extension); } extension.AddChild(element); return true; }
/// <summary> /// Stores the attribute in an extension. /// </summary> /// <param name="extensible">The object that is being extended.</param> /// <param name="attribute">The attribute to store.</param> /// <returns>This method always returns true.</returns> public bool StoreAttribute(IExtensible extensible, IExtensionAttribute attribute) { TestExtension extension; extension = extensible.Extensions.FirstOrDefault((e) => e.Name == TestExtension.ExtensionName) as TestExtension; if (extension == null) { extension = new TestExtension(); extensible.Extensions.Add(extension); } extension.AddAttribute(attribute); return true; }
/// <summary> /// Stores the element in an extension. /// </summary> /// <param name="extensible">The object that is being extended.</param> /// <param name="element">The element to store.</param> /// <returns>This method always returns true.</returns> public bool StoreElement(IExtensible extensible, ElementInfo element) { TestExtension extension; extension = extensible.Extensions.FirstOrDefault((e) => e.Name == TestExtension.ExtensionName) as TestExtension; if (extension == null) { extension = new TestExtension(); extensible.Extensions.Add(extension); } extension.AddChild(element); return(true); }
/// <summary> /// Stores the attribute in an extension. /// </summary> /// <param name="extensible">The object that is being extended.</param> /// <param name="attribute">The attribute to store.</param> /// <returns>This method always returns true.</returns> public bool StoreAttribute(IExtensible extensible, IExtensionAttribute attribute) { TestExtension extension; extension = extensible.Extensions.FirstOrDefault((e) => e.Name == TestExtension.ExtensionName) as TestExtension; if (extension == null) { extension = new TestExtension(); extensible.Extensions.Add(extension); } extension.AddAttribute(attribute); return(true); }
/// <summary> /// Demonstrates how to store custom attributes and elements on a <see cref="File"/> element using a custom /// extension and element types. /// </summary> public static void StoreCustomExtension() { TestExtension extension; IExtensible extensible; Segment segment; XliffDocument document; XliffReader reader; Unit unit; string path; // This namespace will be stored on the document element like: <xliff xmlns:pre1="urn:custom:extension:1.0" const string customNamespace = "urn:custom:extension:1.0"; const string customPrefix = "customPrefix"; extension = new TestExtension(); document = new XliffDocument("en-us"); document.Files.Add(new File("f1")); unit = new Unit("u1"); document.Files[0].Containers.Add(unit); segment = new Segment("s1"); unit.Resources.Add(segment); segment.Source = new Source(); segment.Source.Text.Add(new PlainText("text")); extensible = document.Files[0]; // Create custom attributes that look like: <file id="f1" pre1:testattr1="testvalue1" pre1:testattr2="testvalue2"> if (extensible.SupportsAttributeExtensions) { extension.AddAttribute(new TestAttribute(customPrefix, customNamespace, "testattr1", "testvalue1")); extension.AddAttribute(new TestAttribute(customPrefix, customNamespace, "testattr2", "testvalue2")); extensible.Extensions.Add(extension); } // Create a custom element that looks like: <pre1:testelement1 pre1:testattr1="testvalue1" /> if (extensible.SupportsElementExtensions) { ElementInfo info; TestElement element; element = new TestElement(); element.SetAttribute(customPrefix, customNamespace, "testattr1", "testvalue1"); info = new ElementInfo(new XmlNameInfo(customPrefix, customNamespace, "testelement1"), element); extension.AddChild(info); } // Write the file just like any other file. path = IO.Path.GetTempFileName(); SampleCode.WriteDocument(document, path); // Read the file using an custom extension handler so the custom types are loaded. The loaded File will // have the custom extension and attributes and elements on it just like it was created above. reader = new XliffReader(); reader.RegisterExtensionHandler(customNamespace, new TestExtensionHandler()); using (IO.FileStream stream = new IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read)) { document = reader.Deserialize(stream); } }