protected override object GetValueFromXml(XElement root, XName name, PropertyInfo prop) { var isAttribute = false; // Check for the DeserializeAs attribute on the property var options = prop.GetAttribute<DeserializeAsAttribute>(); if (options != null) { name = options.Name ?? name; isAttribute = options.Attribute; } if (isAttribute) { var attributeVal = GetAttributeByName(root, name); if (attributeVal != null) { return attributeVal.Value; } } return base.GetValueFromXml(root, name, prop); }
/// <summary> /// Writes the object to an XElement instance using the specified root element name. /// </summary> /// <param name="rootElement">Root element name</param> /// <returns>XElement representation of the object</returns> public override XElement WriteToXml(XName rootElement) { return new XElement(rootElement, from provider in _providers.ProviderDictionary.Values select provider.WriteToXml( DefaultResourceConnectionProviderRegistry.GetProviderName(provider.GetType()))); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyManager"/> class. /// </summary> public PropertyManager(string schema) { keyElementName = XName.Get("Key", schema); entityTypeName = XName.Get(entityTypeNameString, schema); entityContainerName = XName.Get("EntityContainer", schema); entitySetName = XName.Get("EntitySet", schema); }
public static void SetValue(XElement _parent, PropertyExtensionContext _context, XName xName, bool value) { bool propertyValue = value; // Make changes to the .edmx document in an EntityDesignerChangeScope to enable undo/redo of changes. using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set EDMXFileTools")) { if (_parent.HasElements) { XElement lastChild = _parent.Elements().Where<XElement>(element => element != null && element.Name == xName).LastOrDefault(); if (lastChild != null) { // Property element already exists under the EntityType element, so update its value. lastChild.SetValue(propertyValue.ToString()); } else { // Property element does not exist, so create a new one as the last child of the EntityType element. _parent.Elements().Last().AddAfterSelf(new XElement(xName, propertyValue.ToString())); } } else { // The element has no child elements so create a new MyNewProperty element as its first child. _parent.Add(new XElement(xName, propertyValue.ToString())); } // Commit the changes. scope.Complete(); } }
public static string GetElementValue(this XContainer elem, XName elementName) { XElement childElement = elem.Element(elementName); if (childElement != null) return childElement.Value; return null; }
public static XAttribute TryGetAttribute(this XElement element, XName attributeName) { var attribute = element.Attribute(attributeName) ?? element.Attributes().SingleOrDefault( a => a.Name.ToString().Homogenize() == attributeName.ToString().Homogenize()); return attribute ?? new XAttribute(attributeName, string.Empty); }
public XElement SerializeRoot(object obj, Type rootType, XName name, XSerializerNamespaceCollection namespaces, SerializationScope globalScope) { Debug.Assert(rootType.IsInstanceOfType(obj)); Debug.Assert(namespaces != null); var root = SerializeXElement(obj, rootType, name, globalScope); //导入命名空间。 foreach (var ns in namespaces) root.SetAttributeValue(XNamespace.Xmlns + ns.Prefix, ns.Uri); //处理导入的类型。 var nsCounter = 0; foreach (var descendant in root.Descendants()) { var actualTypeName = descendant.Annotation<XName>(); if (actualTypeName != null) { if (actualTypeName.Namespace == descendant.GetDefaultNamespace()) { descendant.SetAttributeValue(SerializationHelper.Xsi + "type", actualTypeName.LocalName); } else { var prefix = descendant.GetPrefixOfNamespace(actualTypeName.Namespace); if (prefix == null) { nsCounter++; prefix = "nss" + nsCounter; descendant.SetAttributeValue(XNamespace.Xmlns + prefix, actualTypeName.NamespaceName); } descendant.SetAttributeValue(SerializationHelper.Xsi + "type", prefix + ":" + actualTypeName.LocalName); } descendant.RemoveAnnotations<XName>(); } } return root; }
public static IEnumerable<XElement> Ancestors(this XNode node, XName name, bool ignoreNamespace) { if (ignoreNamespace) return node.Ancestors().Where(e => e.Name.LocalName == name.LocalName); else return node.Ancestors(name); }
internal XElement ExecuteFSMSubGroup(IEnumerator<XElement> enumerator, XName[] namesInList) { Debug.Assert(namesInList != null); XElement currElem = null; WildCard matchingWildCard = null; XName matchingName = null; while(enumerator.MoveNext()){ currElem = enumerator.Current; currentState = FsmMakeTransition(currentState, currElem.Name, out matchingName, out matchingWildCard); if (currentState!= FSM.InvalidState) { if ( matchingName != null) for(int i =0; i < namesInList.Length; i++) { if (namesInList.GetValue(i).Equals(currElem.Name)) return currElem; } } else {//Get stuck. No recovery attempt is provided for now. return null; } } //No matching elements/wildcards are found return null; }
//TODO Use PackageProperties. internal OpenXmlSdkDocumentProperties(OpenXmlSdkTextDocument document) { if (document == null) { throw new ArgumentNullException("document"); } _document = document; _xCorePropertiesNamespace = CorePropertiesSchema; _xDublinCoreNamespace = DublinCoreSchema; _xDCTermsNamespace = DublinCoreTermsSchema; _xCreatedElementName = _xDCTermsNamespace + "created"; _xCreatorElementName = _xDublinCoreNamespace + "creator"; _xDescriptionElementName = _xDublinCoreNamespace + "description"; _xKeywordsElementName = _xCorePropertiesNamespace + "keywords"; _xLanguageElementName = _xDublinCoreNamespace + "language"; _xLastModifiedByElementName = _xCorePropertiesNamespace + "lastModifiedBy"; _xLastPrintedElementName = _xCorePropertiesNamespace + "lastPrinted"; _xModifiedElementName = _xDCTermsNamespace + "modified"; _xRevisionElementName = _xCorePropertiesNamespace + "revision"; _xSubjectElementName = _xDublinCoreNamespace + "subject"; _xTitleElementName = _xDublinCoreNamespace + "title"; using (Stream stream = document.InnerObject.CoreFilePropertiesPart.GetStream()) { _xDocument = XDocument.Load(stream); } }
public static void SetElementValueConditionalBool(this SXL.XElement el, SXL.XName name, bool?value) { if (value != null) { el.SetElementValue(name, value.Value ? "1" : "0"); } }
internal XElement GenerateXElement(XName name) { XNamespace xmlns = name.Namespace; XElement element = new XElement(name); return element; }
/// <summary> /// Writes the object to an XElement instance using the specified root element name. /// </summary> /// <param name="rootElement">Root element name</param> /// <returns>XElement representation of the object</returns> public override XElement WriteToXml(XName rootElement) { return new XElement (ROOT, new XElement( LIFETIMEMANAGERS, from manager in _managers.Values select new XElement( MANAGER, new XAttribute(ALIAS, manager.Alias), new XAttribute(TYPE, manager.Type), from par in manager.Parameters select new XElement(PARAM, // ReSharper disable AssignNullToNotNullAttribute new XAttribute(TYPE, par.Type .AssemblyQualifiedName), // ReSharper restore AssignNullToNotNullAttribute new XAttribute(VALUE, par.Value)))), new XElement( MAPPINGS, from map in _mappings.Values select new XElement( MAP, new XAttribute(SERVICE, map.Service), new XAttribute(IMPLEMENTATION, map.Implementation), new XAttribute(LIFETIME, map.LifetimeManager), map.ConstructorParameters.WriteToXml(CONSTRUCT), map.Properties.WriteToXml(PROPERTIES)))); }
public static void SetElementValueConditional <T>(this SXL.XElement el, SXL.XName name, T?value) where T : struct { if (value != null) { el.SetElementValue(name, value.ToString()); } }
public static string GetDescendantValue(this XElement container, XName name) { var node = container.Descendants(name) .FirstOrDefault(); return node == null ? null : node.Value; }
/// <summary> /// Adds a new attribute to the element /// Does not permit modification of an existing attribute. /// Does not add empty or null attributes or values. /// </summary> /// <param name="element">The element to add the attribute to</param> /// <param name="attribute">The attribute to add</param> /// <param name="value">the value of the attribute to add</param> /// <returns>The element passed in. (Permits fluent usage)</returns> internal static XElement AddAttribute(this XElement element, XName attribute, string value) { if (element == null) { return null; } // we quietly ignore attempts to add empty data or attributes. if (string.IsNullOrWhiteSpace(value) || attribute == null || string.IsNullOrWhiteSpace(attribute.ToString())) { return element; } // Swidtag attributes can be added but not changed -- if it already exists, that's not permitted. var current = element.GetAttribute(attribute); if (!string.IsNullOrWhiteSpace(current)) { if (value != current) { throw new Exception("Attempt to change Attribute '{0}' present in element '{1}'".format(attribute.LocalName, element.Name.LocalName)); } // if the value was set to that already, don't worry about it. return element; } element.SetAttributeValue(attribute, value); return element; }
private static XDocument CreateDocument(XName rootName, IFileSystem fileSystem, string path) { XDocument document = new XDocument(new XElement(rootName)); // Add it to the file system fileSystem.AddFile(path, document.Save); return document; }
public XElement (XName name, params object [] content) { if (name == null) throw new ArgumentNullException ("name"); this.name = name; Add (content); }
public static void SetValue(XElement _parent, PropertyExtensionContext _context, XName xName, string value) { string propertyValue; if (value != null) propertyValue = value.Trim(); else propertyValue = string.Empty; using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set EDMXFileTools")) { if (_parent.HasElements) { XElement lastChild = _parent.Elements().Where<XElement>(element => element != null && element.Name == xName).LastOrDefault(); if (lastChild != null) { lastChild.SetValue(propertyValue); } else { // MyNewProperty element does not exist, so create a new one as the last // child of the EntityType element. _parent.Elements().Last().AddAfterSelf(new XElement(xName, propertyValue)); } } else { // The EntityType element has no child elements so create a new MyNewProperty // element as its first child. _parent.Add(new XElement(xName, propertyValue)); } // Commit the changes. scope.Complete(); } }
/// <summary> /// Creates a new extension function definition. /// </summary> /// <param name="name"></param> /// <param name="minArgs"></param> /// <param name="maxArgs"></param> /// <param name="argTypes"></param> /// <param name="returnType"></param> /// <param name="fn"></param> public TriflesXPathExtensionFunction(XName name, int minArgs, int maxArgs, XPathResultType[] argTypes, XPathResultType returnType, TriflesXPathInvokable fn) { FunctionName = Checker.NotNull(name, "name"); Minargs = minArgs; Maxargs = maxArgs; if (Minargs < 0) { throw new ArgumentOutOfRangeException("minArgs"); } else if (Maxargs < Minargs) { throw new ArgumentOutOfRangeException("maxArgs", "maxArgs cannot be less than minArgs"); } if (argTypes == null) { argTypes = new XPathResultType[0]; } this.argTypes = argTypes.ToImmutableArray(); ReturnType = Checker.NotNull(returnType, "returnType"); invokable = Checker.NotNull(fn, "fn"); }
/// <summary> /// Copy ctor /// </summary> public XAttribute(XAttribute source) { if (source == null) throw new ArgumentNullException("source"); value = source.value; name = source.name; }
public static void SetElementValueConditional(this SXL.XElement el, SXL.XName name, string value) { if (value != null) { el.SetElementValue(name, value); } }
public static string GetStringAttribute(this XElement element, XName name) { XAttribute xattribute = element.Attribute(name); if (xattribute != null) return xattribute.Value; return string.Empty; }
public static IEnumerable<XElement> ElementsAfterSelf(this XContainer node, XName name, bool ignoreNamespace) { if (ignoreNamespace) return node.ElementsAfterSelf().Where(e => e.Name.LocalName == name.LocalName); else return node.ElementsAfterSelf(name); }
internal static string AttributeValue(this XElement xml, XName attributeName) { var attribute = xml.Attribute(attributeName); if (null == attribute) return null; return attribute.Value; }
public static IEnumerable<XElement> Descendants(this XContainer container, XName name, bool ignoreNamespace) { if (ignoreNamespace) return container.Descendants().Where(e => e.Name.LocalName == name.LocalName); else return container.Descendants(name); }
public static string GetAttribute(this XElement elem, XName attributeName) { XAttribute attribute = elem.Attribute(attributeName); if (attribute != null) return attribute.Value; return null; }
internal XElement ExecuteFSM(IEnumerator<XElement> enumerator, XName requestingXName, WildCard requestingWildCard) { XElement currElem = null; WildCard matchingWildCard = null; XName matchingName = null; while(enumerator.MoveNext()){ currElem = enumerator.Current; currentState = FsmMakeTransition(currentState, currElem.Name, out matchingName, out matchingWildCard); if (currentState!= FSM.InvalidState) { if ( (requestingXName != null) && (matchingName != null)) { if (requestingXName.Equals(currElem.Name)) return currElem; } else if ( (requestingWildCard != null) && (matchingWildCard != null) ){//requesting for ANY if (requestingWildCard.Allows(currElem.Name)) //Make sure current element is allowed by requesting ANY property return currElem; } } else {//Get stuck. No recovery attempt is provided for now. return null; } } //No matching elements/wildcards are found return null; }
public static string TryGetAttributeValue(this XElement element, XName attributeName) { var attribute = element.Attribute(attributeName) ?? element.Attributes().SingleOrDefault( a => a.Name.ToString().Homogenize() == attributeName.ToString().Homogenize()); return attribute == null ? null : attribute.Value; }
public XElement this[XName section] { get { return root.Element(section) ?? new XElement(section); } }
public static string GetAttribute(this XElement el, XName name, string defaultValue = "") { var attr = el.Attribute(name); if (attr != null) return attr.Value; return defaultValue; }
public static string GetAttributeValue(this XElement element, XName name) { XAttribute xAttribute = element.Attribute(name); if (xAttribute != null) { return xAttribute.Value; } return null; }
public static XElement Element(this XContainer node, XName name, bool ignoreNamespace) { if (ignoreNamespace) return node.Elements().FirstOrDefault(e => e.Name.LocalName == name.LocalName); else return node.Element(name); }
private List <XElement> GetElementsByTagName(string TagName) { System.Xml.Linq.XName TName = System.Xml.Linq.XName.Get(TagName, XD.Root.Name.Namespace.NamespaceName); var c = from i in XD.Elements().Descendants().Elements(TName) select i; return(c.ToList()); }
public static string GetAttributeValue(this SXL.XElement el, SXL.XName name, string defval) { var attr = el.Attribute(name); if (attr == null) { return defval; } return attr.Value; }
public static void SetElementValueConditionalDateTime(this SXL.XElement el, SXL.XName name, System.DateTimeOffset?date) { const string datefmt = "yyyy-MM-ddTHH:mm:ss"; if (date != null) { var culture = System.Globalization.CultureInfo.InvariantCulture; string datestr = date.Value.ToString(datefmt, culture); el.SetElementValue(name, datestr); } }
public static T GetAttributeValue<T>(this SXL.XElement el, SXL.XName name, T defval, System.Func<string, T> converter) { var a = el.Attribute(name); if (a == null) { return defval; } string v = a.Value; return converter(v); }
public static string GetAttributeValue(SXL.XElement el, SXL.XName name, string defval) { var attr = el.Attribute(name); if (attr == null) { return(defval); } return(attr.Value ?? defval); }
static void MoveAttributeIfAny(System.Xml.Linq.XName attributeName, System.Xml.Linq.XElement from, System.Xml.Linq.XElement to) { System.Xml.Linq.XAttribute attr = from.Attribute(attributeName); if (attr != null) { // Remove the attribute from its current parent: attr.Remove(); // Add the attribute to its new parent to.Add(attr); } }
public static void AddElementValue(this System.Xml.Linq.XElement el, System.Xml.Linq.XName name, string value) { var new_el = new System.Xml.Linq.XElement(name); el.Add(new_el); if (!string.IsNullOrEmpty(value)) { new_el.AddText(value); } }
public static T GetAttributeValue<T>(this SXL.XElement el, SXL.XName name, System.Func<string, T> converter) { var a = el.Attribute(name); if (a == null) { var culture = System.Globalization.CultureInfo.InvariantCulture; string msg = string.Format(culture, "Missing value for attribute \"{0}\"", name); throw new System.ArgumentException(msg); } string v = a.Value; return converter(v); }
static bool TryGetType(System.Xml.Linq.XName xName, out Type outputType) { //todo: take advantage of the information stored inside the XName structure to better resolve the type. string nameToResolve = xName.LocalName; Assembly[] assembliesToLookInto = new Assembly[] { typeof(Button).Assembly, // Note: this is "PresentationFramework" typeof(SolidColorBrush).Assembly, // Note: this is "PresentationCore" typeof(Matrix).Assembly, // Note: this is "WindowsBase" }; if (_typesAlreadyFound.ContainsKey(nameToResolve)) { outputType = _typesAlreadyFound[nameToResolve]; return(true); } else { foreach (var assembly in assembliesToLookInto) { foreach (Type type in assembly.GetTypes()) { if (type.Name == nameToResolve) { outputType = type; _typesAlreadyFound.Add(nameToResolve, type); return(true); } } } outputType = null; return(false); /* * string fullQualifiedNameTemplate = (new Button().GetType().AssemblyQualifiedName).Replace("Button", "{0}"); * Type elementType = System.Type.GetType(String.Format(fullQualifiedNameTemplate, nameToResolve)); * if (elementType != null) * { * outputType = elementType; * _typesAlreadyFound.Add(nameToResolve, elementType); * return true; * } * else * { * outputType = null; * return false; * } */ } }
public static SXL.XElement RemoveElement(this SXL.XElement el, SXL.XName name) { var n = el.Element(name); if (n != null) { n.Remove(); return(n); } else { return(null); } }
public static SXL.XElement CleanElement(this SXL.XElement el, SXL.XName name) { var n = el.Element(name); if (n != null) { var nodes = n.Nodes().ToList(); foreach (var c in nodes) { c.Remove(); } return(n); } else { return(null); } }
// Converts XML data back to the original form. private IDictionary <XName, InstanceValue> XmlToDictionary(XmlDocument doc) { IDictionary <System.Xml.Linq.XName, InstanceValue> data = new Dictionary <System.Xml.Linq.XName, InstanceValue>(); NetDataContractSerializer s = new NetDataContractSerializer(); XmlNodeList instances = doc.GetElementsByTagName("InstanceValue"); foreach (XmlElement instanceElement in instances) { XmlElement keyElement = (XmlElement)instanceElement.SelectSingleNode("descendant::key"); System.Xml.Linq.XName key = (System.Xml.Linq.XName)DeserializeObject(s, keyElement); XmlElement valueElement = (XmlElement)instanceElement.SelectSingleNode("descendant::value"); object value = DeserializeObject(s, valueElement); InstanceValue instVal = new InstanceValue(value); data.Add(key, instVal); } return(data); }
// Converts XML data back to the original form. private IDictionary <XName, InstanceValue> XmlToDictionary(string xml) { var doc = new XmlDocument(); doc.Load(new StringReader(xml)); IDictionary <System.Xml.Linq.XName, InstanceValue> data = new Dictionary <System.Xml.Linq.XName, InstanceValue>(); XmlNodeList instances = doc.GetElementsByTagName("InstanceValue"); foreach (XmlElement instanceElement in instances) { XmlElement keyElement = (XmlElement)instanceElement.SelectSingleNode("descendant::key"); System.Xml.Linq.XName key = (System.Xml.Linq.XName)DeserializeObject(keyElement); XmlElement valueElement = (XmlElement)instanceElement.SelectSingleNode("descendant::value"); object value = DeserializeObject(valueElement); InstanceValue instVal = new InstanceValue(value); data.Add(key, instVal); } return(data); }
/// <summary> /// Returns the Descendant <see cref="XElement"/>s with the passed in <see cref="XName"/> as an IEnumerable /// of XElement. /// </summary> /// <param name="name">The <see cref="XName"/> to match against descendant <see cref="XElement"/>s.</param> /// <returns>An <see cref="IEnumerable"/> of <see cref="XElement"/></returns> public IEnumerable <XElement> Descendants(XName name) { return(name != null?GetDescendants(name, false) : XElement.EmptySequence); }
/// <summary> /// Returns the child elements of this <see cref="XContainer"/> that match the <see cref="XName"/> passed in. /// </summary> /// <param name="name"> /// The <see cref="XName"/> to match against the <see cref="XElement"/> children of this <see cref="XContainer"/>. /// </param> /// <returns> /// An <see cref="IEnumerable"/> of <see cref="XElement"/> children of this <see cref="XContainer"/> that have /// a matching <see cref="XName"/>. /// </returns> public IEnumerable <XElement> Elements(XName name) { return(name != null?GetElements(name) : XElement.EmptySequence); }
public static T AttributeValue <T>(this System.Xml.Linq.XElement el, System.Xml.Linq.XName name, T defval, System.Func <string, T> converter) { return(LinqXmlUtil.GetAttributeValue(el, name, defval, converter)); }
public static string AttributeValue(this System.Xml.Linq.XElement el, System.Xml.Linq.XName name, string defval) { return(LinqXmlUtil.GetAttributeValue(el, name, defval)); }
public static void SetAttributeValueDouble(this SXL.XElement el, SXL.XName name, double value) { var culture = System.Globalization.CultureInfo.InvariantCulture; el.SetAttributeValue(name, value.ToString(culture)); }
public static void SetElementValueConditional <T, TDest>(this SXL.XElement el, SXL.XName name, T?value, System.Func <T, TDest> xfrm) where T : struct { if (value != null) { var v = xfrm(value.Value); el.SetElementValue(name, v.ToString()); } }
public IEnumerable <XElement> AncestorsAndSelf(XName name) { return(GetAncestorList(name, true)); }