Esempio n. 1
0
        /// <summary>
        /// Creates and returns the attribute with the given name in the location
        /// specified by the given location string in the given XML element.
        /// </summary>
        /// <param name="baseElement">The XML element.</param>
        /// <param name="location">The location string.</param>
        /// <param name="attrName">Name of the attribute.</param>
        /// <param name="attrValue">The value to be assigned to the attribute.</param>
        /// <param name="documentDefaultNamespace">The default namespace.</param>
        /// <returns>returns the attribute with the given name in the location
        /// specified by the given location string in the given XML element.</returns>
        public static XAttribute CreateAttribute(XElement baseElement, string location, XName attrName, object attrValue, XNamespace documentDefaultNamespace)
        {
            XElement newLoc = FindLocation(baseElement, location);

            if (newLoc == null)
            {
                if (CanCreateLocation(baseElement, location))
                {
                    newLoc = CreateLocation(baseElement, location);
                }
                else
                {
                    return(null);
                }
            }

            // check if the attribute does not exist
            if (attrName.Namespace.IsEmpty() && attrName.Namespace != documentDefaultNamespace)
            {
                // i.e., the attribute already exists
                if (newLoc.Attribute(attrName) != null)
                {
                    return(null); // we cannot create another one with the same name
                }
            }
            else
            {
                if (newLoc.Attribute_NamespaceNeutral(attrName) != null) // i.e., the attribute already exists
                {
                    return(null);                                        // we cannot create another one with the same name
                }
            }

            return(newLoc.AddAttributeNamespaceSafe(attrName, attrValue, documentDefaultNamespace));
        }
Esempio n. 2
0
 /// <summary>
 ///     Adds the 'xml:space="preserve"' attribute to the specified element.
 /// </summary>
 /// <param name="element">Element to add the 'xml:space="preserve"' attribute to</param>
 /// <param name="culture">The <see cref="CultureInfo"/> to use for string-formatting values.</param>
 /// <returns></returns>
 public static XElement AddPreserveSpaceAttribute(XElement element, CultureInfo culture)
 {
     element.AddAttributeNamespaceSafe(XNamespace.Xml + "space", "preserve", null, culture);
     return(element);
 }