InsertAttribute() public method

Inserts a new attribute with the specified name and value.
public InsertAttribute ( string name, string value ) : void
name string The name of the new attribute.
value string The value of the new attribute.
return void
コード例 #1
0
        internal override void StoreValue(Element element, object serialized, object source, TypeCache typeCache, Cache cache)
        {
            Node.Attribute attribute;
            if (!GetNodeFromQuery(Query, CreateQuery, element, out attribute))
            {
                attribute = element.Attribute(Name);
            }
            else if (attribute == null)
            {
                return;
            }

            string value = (string) serialized;
            if (attribute == null && value != null)
            {
                element.InsertAttribute(Name, value);
            }
            else if (attribute != null && value == null)
            {
                attribute.Remove();
            }
            else if (attribute != null)
            {
                attribute.Value = value;
            }
        }
コード例 #2
0
        internal override void Store(Element element, object serialized, object source, TypeCache typeCache, Cache cache)
        {
            if (source == null || serialized == null) return;

            // Replace the element content in the database with the new content
            using (TextReader textReader = new StringReader((string)serialized))
            {
                using(XmlReader reader = XmlReader.Create(textReader))
                {
                    // Move to the root element
                    reader.MoveToContent();

                    // Replace all attributes
                    element.RemoveAllAttributes();
                    if(reader.HasAttributes)
                    {
                        while(reader.MoveToNextAttribute())
                        {
                            element.InsertAttribute(reader.Name, reader.Value);
                        }
                        reader.MoveToElement();
                    }

                    // Replace the child content
                    // Need to use an intermediate string since there is no way to
                    // get the "inner XML" of an XmlReader without getting the
                    // parent element too
                    element.InnerXml = reader.ReadInnerXml();
                }
            }
        }