System.Xml.XmlDataDocument.CreateElement C# (CSharp) Méthode

CreateElement() public méthode

Creates an element with the specified Prefix, LocalName, and NamespaceURI.
public CreateElement ( string prefix, string localName, string namespaceURI ) : XmlElement
prefix string
localName string
namespaceURI string
Résultat XmlElement
        public override XmlElement CreateElement(string prefix, string localName, string namespaceURI)
        {
            // There are three states for the document:
            //  - special listeners ON, no permananent listeners: this is when the data doc was created w/o any dataset, and the 1st time a new row/element
            //    is created we should subscribe the permenent listeners.
            //  - special listeners OFF, permanent listeners ON: this is when the data doc is loaded (from dataset or XML file) and synchronization takes place.
            //  - special listeners OFF, permanent listeners OFF: this is then the data doc is LOADING (from dataset or XML file) - the synchronization is done by code,
            //    not based on listening to events.
#if DEBUG
            // Cannot have both special and permananent listeners ON
            if (_fDataRowCreatedSpecial)
                Debug.Assert((_fBoundToDataSet == false) && (_fBoundToDocument == false));
            // fBoundToDataSet and fBoundToDocument should have the same value
            Debug.Assert(_fBoundToDataSet ? _fBoundToDocument : (!_fBoundToDocument));
#endif
            if (prefix == null)
                prefix = string.Empty;
            if (namespaceURI == null)
                namespaceURI = string.Empty;

            if (!_fAssociateDataRow)
            {
                // Loading state: create just the XmlBoundElement: the LoadTreeFromDataSet/LoadDataSetFromTree will take care of synchronization
                return new XmlBoundElement(prefix, localName, namespaceURI, this);
            }

            // This is the 1st time an element is beeing created on an empty XmlDataDocument - unbind special listeners, bind permanent ones and then go on w/
            // creation of this element
            EnsurePopulatedMode();
            Debug.Assert(_fDataRowCreatedSpecial == false);

            // Loaded state: create a DataRow, this in turn will create and associate the XmlBoundElement, which we will return.
            DataTable dt = _mapper.SearchMatchingTableSchema(localName, namespaceURI);
            if (dt != null)
            {
                DataRow row = dt.CreateEmptyRow();
                // We need to make sure all fields are DBNull
                foreach (DataColumn col in dt.Columns)
                {
                    if (col.ColumnMapping != MappingType.Hidden)
                        SetRowValueToNull(row, col);
                }
                XmlBoundElement be = row.Element;
                Debug.Assert(be != null);
                be.Prefix = prefix;
                return be;
            }
            // No matching table schema for this element: just create the element
            return new XmlBoundElement(prefix, localName, namespaceURI, this);
        }

Usage Example

Exemple #1
0
        public void AddLink(string title, string uri)
        {
            XmlDocument doc = new XmlDataDocument();
            doc.Load("RssLinks.xml");
            XmlNode rootNode = doc.SelectSingleNode("links");

            XmlNode linkNode = doc.CreateElement("link");

            XmlNode titleNode = doc.CreateElement("title");
            XmlText titleText = doc.CreateTextNode(title);
            titleNode.AppendChild(titleText);

            XmlNode uriNode = doc.CreateElement("uri");
            XmlText uriText = doc.CreateTextNode(uri);
            uriNode.AppendChild(uriText);

            XmlNode defaultshowNode = doc.CreateElement("defaultshow");
            XmlText defaultshowText = doc.CreateTextNode("false");
            defaultshowNode.AppendChild(defaultshowText);

            linkNode.AppendChild(titleNode);
            linkNode.AppendChild(uriNode);
            linkNode.AppendChild(defaultshowNode);

            rootNode.AppendChild(linkNode);

            doc.Save("RssLinks.xml");
        }
All Usage Examples Of System.Xml.XmlDataDocument::CreateElement
XmlDataDocument