/// <summary> /// Remove the attribute with the given name from the collection. /// </summary> /// <param name="attributes">Attribute collection.</param> /// <param name="attributeName">Name of attribute to remove.</param> public static void Remove(this XmlAttributeCollection attributes, string attributeName) { if (attributes == null) { throw new ArgumentNullException(nameof(attributes)); } if (attributeName == null) { throw new ArgumentNullException(nameof(attributeName)); } var attribute = attributes[attributeName]; attributes.Remove(attribute); }
public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); //Create an attribute collection and remove an attribute //from the collection. XmlAttributeCollection attrColl = doc.DocumentElement.Attributes; attrColl.Remove(attrColl["genre"]); Console.WriteLine("Display the modified XML...\r\n"); Console.WriteLine(doc.OuterXml); }
public void RemoveCanDeleteLastAttr() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute attr1, attr2, attr3; attr1 = element.Attributes.Append(doc.CreateAttribute("attr1")); attr2 = element.Attributes.Append(doc.CreateAttribute("attr2")); attr3 = element.Attributes.Append(doc.CreateAttribute("attr3")); XmlAttributeCollection target = element.Attributes; target.Remove(attr3); Assert.Equal(2, target.Count); Assert.Same(attr1, target[0]); Assert.Same(attr2, target[1]); }
public void Remove() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml("<root a1='garnet' a2='amethyst' a3='bloodstone' a4='diamond' a5='emerald' a6='pearl' a7='ruby' a8='sapphire' a9='moonstone' a10='opal' a11='topaz' a12='turquoize' />"); XmlElement el = xmlDoc.DocumentElement; XmlAttributeCollection col = el.Attributes; // Remove XmlAttribute attr = col.Remove(el.GetAttributeNode("a12")); Assert.AreEqual(11, col.Count, "Remove"); Assert.AreEqual("a12", attr.Name, "Remove.Removed"); // RemoveAt attr = col.RemoveAt(5); Assert.AreEqual(null, el.GetAttributeNode("a6"), "RemoveAt"); Assert.AreEqual("pearl", attr.Value, "Remove.Removed"); }
///<summary> /// Delete an attribute from the given target node. ///</summary> public bool DeleteNodeAttribute(XmlNode targetNode, string sAttributeName) { bool bResult = false; try { XmlAttributeCollection attrColl = targetNode.Attributes; XmlAttribute xmlAttribute = attrColl.Remove(attrColl[sAttributeName, ""]); if (xmlAttribute != null) { bResult = true; } } catch (Exception e) { HandleException(e); } return(bResult); }
private string GetAttribute(XmlAttributeCollection attrs, string attr, bool required, XmlNode node) { XmlAttribute a = attrs[attr]; string r = null; if (a != null) { r = a.Value; if (required) { ValidateAttribute(attr, r, node); } attrs.Remove(a); } else if (required) { ThrowMissingAttribute(attr, node); } return(r); }
private void dgvTranslate_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (_blnLoading || e.RowIndex < 0) { return; } DataGridViewRow item = dgvTranslate.Rows[e.RowIndex]; TranslatedIndicator(item); string strTranslated = item.Cells["Text"].Value.ToString(); string strEnglish = item.Cells["English"].Value.ToString(); bool blnSetTranslatedAttribute = strTranslated != strEnglish && Convert.ToBoolean(item.Cells["Translated?"].Value); string strKey = item.Cells["Key"].Value.ToString(); XmlNode xmlNodeLocal = _objTranslationDoc.SelectSingleNode("/chummer/strings/string[key = " + strKey.CleanXPath() + "]"); if (xmlNodeLocal != null) { XmlElement xmlElement = xmlNodeLocal["text"]; if (xmlElement != null) { xmlElement.InnerText = strTranslated; } XmlAttributeCollection objAttributes = xmlNodeLocal.Attributes; if (objAttributes != null) { XmlAttribute objAttrib = objAttributes["translated"]; if (objAttrib != null) { if (blnSetTranslatedAttribute) { objAttrib.Value = bool.TrueString; } else { objAttributes.Remove(objAttrib); } } else if (blnSetTranslatedAttribute) { objAttrib = _objTranslationDoc.CreateAttribute("translated"); objAttrib.Value = bool.TrueString; objAttributes.SetNamedItem(objAttrib); } } } else { XmlNode newNode = _objTranslationDoc.CreateNode(XmlNodeType.Element, "string", null); XmlElement elem = _objTranslationDoc.CreateElement("key"); XmlText xmlString = _objTranslationDoc.CreateTextNode(strKey); newNode.AppendChild(elem); elem.AppendChild(xmlString); elem = _objTranslationDoc.CreateElement("text"); xmlString = _objTranslationDoc.CreateTextNode(strTranslated); newNode.AppendChild(elem); elem.AppendChild(xmlString); XmlAttribute objAttrib = _objTranslationDoc.CreateAttribute("translated"); objAttrib.Value = bool.TrueString; newNode.Attributes?.SetNamedItem(objAttrib); XmlNode root = _objTranslationDoc.SelectSingleNode("/chummer/strings/."); root?.AppendChild(newNode); } TranslatedIndicator(item); Save(_objTranslationDoc, false); }
private void dgvSection_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (_blnLoading || e.RowIndex < 0) { return; } DataGridViewRow item = dgvSection.Rows[e.RowIndex]; TranslatedIndicator(item); string strTranslated = item.Cells["Text"].Value.ToString(); string strEnglish = item.Cells["English"].Value.ToString(); bool blnSetTranslatedAttribute = strTranslated != strEnglish && Convert.ToBoolean(item.Cells["Translated?"].Value); string strId = item.Cells["Id"].Value.ToString(); string strSection = cboSection.Text; if (strSection == "[Show All Sections]") { strSection = "*"; } string strBaseXPath = "/chummer/chummer[@file = " + cboFile.Text.CleanXPath() + "]/" + strSection; if (cboFile.Text == "tips.xml") { XmlNode xmlNodeLocal = _objDataDoc.SelectSingleNode(strBaseXPath + "/*[id = " + strId.CleanXPath() + "]") ?? _objDataDoc.SelectSingleNode(strBaseXPath + "/*[text = " + strEnglish.CleanXPath() + "]"); if (xmlNodeLocal != null) { XmlElement element = xmlNodeLocal["translate"]; if (element != null) { element.InnerText = strTranslated; } XmlAttributeCollection objAttributes = xmlNodeLocal.Attributes; if (objAttributes != null) { XmlAttribute objAttrib = objAttributes["translated"]; if (objAttrib != null) { if (blnSetTranslatedAttribute) { objAttrib.Value = bool.TrueString; } else { objAttributes.Remove(objAttrib); } } else if (blnSetTranslatedAttribute) { objAttrib = _objDataDoc.CreateAttribute("translated"); objAttrib.Value = bool.TrueString; objAttributes.SetNamedItem(objAttrib); } } } } else { XmlNode xmlNodeLocal = _objDataDoc.SelectSingleNode(strBaseXPath + "/*[id = " + strId.CleanXPath() + "]") ?? _objDataDoc.SelectSingleNode(strBaseXPath + "/*[name = " + strEnglish.CleanXPath() + "]"); if (xmlNodeLocal == null) { xmlNodeLocal = _objDataDoc.SelectSingleNode(strBaseXPath + "/*[. = " + strEnglish.CleanXPath() + "]"); XmlAttributeCollection objAttributes = xmlNodeLocal?.Attributes; if (objAttributes != null) { XmlAttribute objAttrib = objAttributes["translated"]; if (objAttrib != null) { objAttrib.InnerText = strTranslated; if (blnSetTranslatedAttribute) { objAttrib.Value = bool.TrueString; } else { objAttributes.Remove(objAttrib); } } else if (blnSetTranslatedAttribute) { objAttrib = _objDataDoc.CreateAttribute("translated"); objAttrib.Value = bool.TrueString; objAttributes.SetNamedItem(objAttrib); } } } else { XmlElement element = xmlNodeLocal["translate"]; if (element != null) { element.InnerText = strTranslated; } if (cboFile.Text != "settings.xml") { element = xmlNodeLocal.Name == "book" ? xmlNodeLocal["altcode"] : xmlNodeLocal["altpage"]; if (element != null) { element.InnerText = item.Cells[cboFile.Text == "books.xml" ? "Code" : "Page"].Value .ToString(); } if (cboFile.Text == "qualities.xml") { string strNameOnPage = item.Cells["NameOnPage"].Value.ToString(); element = xmlNodeLocal["altnameonpage"]; if (string.IsNullOrWhiteSpace(strNameOnPage) && element != null) { xmlNodeLocal.RemoveChild(element); } else { if (element == null) { element = _objDataDoc.CreateElement("altnameonpage"); xmlNodeLocal.AppendChild(element); } element.InnerText = strNameOnPage; } } } XmlAttributeCollection objAttributes = xmlNodeLocal.Attributes; if (objAttributes != null) { XmlAttribute objAttrib = objAttributes["translated"]; if (objAttrib != null) { if (blnSetTranslatedAttribute) { objAttrib.Value = bool.TrueString; } else { objAttributes.Remove(objAttrib); } } else if (blnSetTranslatedAttribute) { objAttrib = _objDataDoc.CreateAttribute("translated"); objAttrib.Value = bool.TrueString; objAttributes.SetNamedItem(objAttrib); } } } } TranslatedIndicator(item); Save(_objDataDoc); }