//public string Admin { get { return _admin; } } internal void LoadValuesFromXml(XmlNode section) { XmlAttributeCollection attrs = section.Attributes; if (attrs["LDAPConnectionString"] != null) { _LDAPConnectionString = attrs["LDAPConnectionString"].Value; attrs.RemoveNamedItem("LDAPConnectionString"); } if (attrs["Approver"] != null) { _approver = attrs["Approver"].Value; attrs.RemoveNamedItem("Approver"); } if (attrs["Reviewer"] != null) { _reviewer = attrs["Reviewer"].Value; attrs.RemoveNamedItem("Reviewer"); } //if (attrs["Admin"] != null) //{ // _admin = attrs["Admin"].Value; // attrs.RemoveNamedItem("Admin"); //} // If there are any further attributes, there's an error! if (attrs.Count > 0) { throw new System.Configuration.ConfigurationErrorsException("There are illegal attributes provided in the section"); } }
/// <summary> /// Delete this attribute /// </summary> /// <returns>true if successful</returns> public bool Delete() { if (_Node != null) { bool exists = false; string Value; GetAttributeAt(_AttrName, out Value, out exists); if (exists) { XmlAttributeCollection attrList = _Node.Attributes; if (attrList != null) { attrList.RemoveNamedItem(_AttrName); } } else { return(false); } } else { throw new InvalidOperationException("No valid node in this Attribute!"); } return(true); }
public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' publicationdate='1997'> " + " <title>Pride And Prejudice</title>" + "</book>"); XmlAttributeCollection attrColl = doc.DocumentElement.Attributes; // Remove the publicationdate attribute. attrColl.RemoveNamedItem("publicationdate"); Console.WriteLine("Display the modified XML..."); Console.WriteLine(doc.OuterXml); }
/// <summary> /// Builds the ExceptionManagementSettings and PublisherSettings structures based on the configuration file. /// </summary> /// <param name="parent">Composed from the configuration settings in a corresponding parent configuration section.</param> /// <param name="configContext">Provides access to the virtual path for which the configuration section handler computes configuration values. Normally this parameter is reserved and is null.</param> /// <param name="section">The XML node that contains the configuration information to be handled. section provides direct access to the XML contents of the configuration section.</param> /// <returns>The ExceptionManagementSettings struct built from the configuration settings.</returns> public object Create(object parent, object configContext, XmlNode section) { try { ExceptionManagementSettings settings = new ExceptionManagementSettings(); // Exit if there are no configuration settings. if (section == null) { return(settings); } XmlNode currentAttribute; XmlAttributeCollection nodeAttributes = section.Attributes; // Get the mode attribute. currentAttribute = nodeAttributes.RemoveNamedItem(EXCEPTIONMANAGEMENT_MODE); if (currentAttribute != null && currentAttribute.Value.ToUpper(CultureInfo.InvariantCulture) == "OFF") { settings.Mode = ExceptionManagementMode.Off; } #region Loop through the publisher components and load them into the ExceptionManagementSettings // Loop through the publisher components and load them into the ExceptionManagementSettings. PublisherSettings publisherSettings; foreach (XmlNode node in section.ChildNodes) { if (node.Name == PUBLISHER_NODENAME) { // Initialize a new PublisherSettings. publisherSettings = new PublisherSettings(); // Get a collection of all the attributes. nodeAttributes = node.Attributes; #region Remove the known attributes and load the struct values // Remove the mode attribute from the node and set its value in PublisherSettings. currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_MODE); if (currentAttribute != null && currentAttribute.Value.ToUpper(CultureInfo.InvariantCulture) == "OFF") { publisherSettings.Mode = PublisherMode.Off; } // Remove the assembly attribute from the node and set its value in PublisherSettings. currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_ASSEMBLY); if (currentAttribute != null) { publisherSettings.AssemblyName = currentAttribute.Value; } // Remove the type attribute from the node and set its value in PublisherSettings. currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_TYPE); if (currentAttribute != null) { publisherSettings.TypeName = currentAttribute.Value; } // Remove the exceptionFormat attribute from the node and set its value in PublisherSettings. currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_EXCEPTIONFORMAT); if (currentAttribute != null && currentAttribute.Value.ToUpper(CultureInfo.InvariantCulture) == "XML") { publisherSettings.ExceptionFormat = PublisherFormat.Xml; } // Remove the include attribute from the node and set its value in PublisherSettings. currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_INCLUDETYPES); if (currentAttribute != null) { publisherSettings.IncludeTypes = LoadTypeFilter(currentAttribute.Value.Split(EXCEPTION_TYPE_DELIMITER)); } // Remove the exclude attribute from the node and set its value in PublisherSettings. currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_EXCLUDETYPES); if (currentAttribute != null) { publisherSettings.ExcludeTypes = LoadTypeFilter(currentAttribute.Value.Split(EXCEPTION_TYPE_DELIMITER)); } #endregion #region Loop through any other attributes and load them into OtherAttributes // Loop through any other attributes and load them into OtherAttributes. for (int i = 0; i < nodeAttributes.Count; i++) { publisherSettings.AddOtherAttributes(nodeAttributes.Item(i).Name, nodeAttributes.Item(i).Value); } #endregion // Add the PublisherSettings to the publishers collection. settings.Publishers.Add(publisherSettings); } } // Remove extra allocated space of the ArrayList of Publishers. settings.Publishers.TrimToSize(); #endregion // Return the ExceptionManagementSettings loaded with the values from the config file. return(settings); } catch (Exception exc) { throw new System.Configuration.ConfigurationErrorsException(resourceManager.GetString("RES_EXCEPTION_LOADING_CONFIGURATION"), exc, section); } }
/// <summary> /// Loads in the configuration information from the passed-in XmlNode /// </summary> internal void LoadValuesFromXml(XmlNode section) { XmlAttributeCollection attrs = section.Attributes; if (attrs["message"] != null) { _message = attrs["message"].Value; attrs.RemoveNamedItem("message"); } if (attrs["favoriteNumber"] != null) { _favoriteNumber = Convert.ToInt32(attrs["favoriteNumber"].Value); attrs.RemoveNamedItem("favoriteNumber"); } if (attrs["showMessageInBold"] != null) { _showMessageInBold = XmlConvert.ToBoolean(attrs["showMessageInBold"].Value); attrs.RemoveNamedItem("showMessageInBold"); } // If there are any further attributes, there's an error! if (attrs.Count > 0) { throw new ConfigurationException("There are illegal attributes provided in the <aspnet1Configuration> section"); } // Now, get the <favoriteColors> child node bool processedFavColors = false; foreach (XmlNode childNode in section.ChildNodes) { if (!processedFavColors && childNode.Name == "favoriteColors") { processedFavColors = true; foreach (XmlNode favColorNode in childNode.ChildNodes) { if (favColorNode.Name == "color") { // Get the text node value XmlNode textNode = favColorNode.ChildNodes[0]; if (textNode == null) { throw new ConfigurationException("You must provide a text value for each <color> element, like <color>Red</color>"); } else { _favColors.Add(textNode.Value); } } else { throw new ConfigurationException("<favoriteColors> can only contain child elements named <color>"); } } } else { throw new ConfigurationException("<aspnet1Configuration> may only contain one child element and it must be named <favoriteColors>"); } } }
public void SaveEncodedFile(string xFile, string xData, bool isMod = false, bool saveNow = false) { // if (!App.m_bxmlCacheEnable || xFile== App.AppLicenseFile // || xFile ==App.AppLicenseFile_OLD || xFile== App.AppInfoFile) { // SaveEncodedFile_old(xFile, xData); return; // } string item = xFile.Split('\\').Last(); XmlDocument d = new XmlDocument(); XMLELEMENT el; try { d.LoadXml(xData); } catch (Exception ex) { string log = ex.Message; xData = xData.ToString().Replace("&", "&").Replace("\"", "'"); d.LoadXml(xData); } if (App.m_xmlCache.ContainsKey(item)) { el = (XMLELEMENT)(App.m_xmlCache[item]); el.doc = d; el.isModified = isMod; App.m_xmlCache[item] = el; } else { CheckXmlCache(); App.m_xmlCache.Add(item, new XMLELEMENT(d, isMod)); } //if (!isMod || !saveNow) return; if (!saveNow) { return; } // file xml in d, chiave in item // [1] creare albero ridotto con i soli nodi modificati XmlDocument doc = new XmlDocument(); doc = StaticUtilities.ExtractMasterFileMod(d); if (doc == null) { doc = StaticUtilities.ExtractTreeMod(d); } if (doc == null) { doc = StaticUtilities.ExtractDatiMod(d); } if (doc == null) { return; // just to be sure } // [2] scrivere dati in dbo.xmlSaveTest using (SqlConnection conn = new SqlConnection(App.connString)) { string query, str; str = doc.OuterXml.Replace("'", "''"); query = string.Format( "insert into xmlSaveTest (guid,data)\n" + "values ('{0}','{1}')", item, str); conn.Open(); SqlCommand cmd = new SqlCommand(query, conn); cmd.CommandTimeout = App.m_CommandTimeout; try { cmd.ExecuteNonQuery(); } catch (Exception ex) { if (!App.m_bNoExceptionMsg) { string msg = "SaveEncodedFile(): errore\n" + ex.Message; MessageBox.Show(msg); } } } // [3] invocare dbo.SaveSingleModified passando guid=item using (SqlConnection conn = new SqlConnection(App.connString)) { conn.Open(); SqlCommand cmd = new SqlCommand("dbo.SaveSingleModified", conn); cmd.Parameters.AddWithValue("@guid", item); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandTimeout = App.m_CommandTimeout; try { cmd.ExecuteNonQuery(); } catch (Exception ex) { if (!App.m_bNoExceptionMsg) { string msg = "dbo.SaveSingleModified: errore\n" + ex.Message; MessageBox.Show(msg); } } } // [4] eliminare flag di modifica dalla cache // SQL elimina XML dalla sua cache XmlAttributeCollection attrColl = d.DocumentElement.Attributes; attrColl.RemoveNamedItem("nodeModified"); attrColl.RemoveNamedItem("idFather"); el = (XMLELEMENT)(App.m_xmlCache[item]); el.doc = d; el.isModified = false; App.m_xmlCache[item] = el; }