private XmlNode CreateAttribute(XmlDocument doc, XmlNode currentNode, String axisName) { String attributeName = axisName.Substring(1); XmlAttributeCollection attributesMap = currentNode.Attributes; XmlNode attribute = attributesMap.GetNamedItem(attributeName); if (attribute == null) { attribute = doc.CreateAttribute(attributeName); attributesMap.SetNamedItem(attribute); } return(attribute); }
public void SetNamedItemWithNewNameAppendsAndReturnsAdded() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; element.Attributes.Append(doc.CreateAttribute("attr", "anotherUri")); element.Attributes.Append(doc.CreateAttribute("anotherAttr", "uri")); XmlAttribute expectedAttr = doc.CreateAttribute("attr", "uri"); XmlAttributeCollection target = element.Attributes; XmlNode actualResult = target.SetNamedItem(expectedAttr); Assert.Equal(3, target.Count); Assert.Same(expectedAttr, target[2]); Assert.Same(expectedAttr, actualResult); }
public void SetNamedItemWithExistingNameReplacesAndReturnsRemoved() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; element.Attributes.Append(doc.CreateAttribute("attr", "anotherUri")); XmlAttribute expectedRemoved = doc.CreateAttribute("attr", "uri"); element.Attributes.Append(expectedRemoved); element.Attributes.Append(doc.CreateAttribute("anotherAttr", "uri")); XmlAttribute expectedAdded = doc.CreateAttribute("attr", "uri"); XmlAttributeCollection target = element.Attributes; XmlNode actualResult = target.SetNamedItem(expectedAdded); Assert.Equal(3, target.Count); Assert.Same(expectedAdded, target[1]); Assert.Same(expectedRemoved, actualResult); }
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; // Add a new attribute to the collection. XmlAttribute attr = doc.CreateAttribute("style"); attr.Value = "hardcover"; attrColl.SetNamedItem(attr); Console.WriteLine("Display the modified XML..."); Console.WriteLine(doc.OuterXml); }
/// <summary> /// Set the string /// </summary> /// <param name="val">Source ascii string value to set from</param> /// <returns>true if successful</returns> public bool SetString(string val) { if (_Doc == null || _Node == null) { throw new System.InvalidOperationException("Invalid element"); } bool exists = false; string Value = null; GetAttributeAt(_AttrName, out Value, out exists); if (false == exists) { Create(null); } XmlAttributeCollection attrList = _Node.Attributes; if (attrList != null) { for (int i = 0; i < attrList.Count; ++i) { string name = attrList.Item(i).Name; if (_AttrName == name) { attrList.Item(i).InnerText = val; return(true); } } XmlAttribute attr = _Doc.CreateAttribute(_AttrName); if (attrList != null && attr != null) { attr.InnerText = val; attrList.SetNamedItem(attr); return(true); } } return(false); }
public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); //Create a new attribute. XmlAttribute newAttr = doc.CreateAttribute("genre"); newAttr.Value = "novel"; //Create an attribute collection and add the new attribute //to the collection. XmlAttributeCollection attrColl = doc.DocumentElement.Attributes; attrColl.SetNamedItem(newAttr); Console.WriteLine("Display the modified XML...\r\n"); Console.WriteLine(doc.OuterXml); }
/// <summary> /// Create this attribute with this optional namespaceUri /// </summary> /// <param name="namespaceUri">namespace URI</param> /// <returns></returns> public bool Create(string namespaceUri) { if (_Doc != null && _Node != null) { bool exists = false; string Value = null; GetAttributeAt(_AttrName, out Value, out exists); if (false == exists) { XmlAttributeCollection attrList = _Node.Attributes; XmlAttribute pAttr = _Doc.CreateAttribute(_AttrName, namespaceUri); if (attrList != null && pAttr != null) { attrList.SetNamedItem(pAttr); } return(true); } } return(false); }
//! Create this attribute with this optional namespaceUri public bool Create(string namespaceUri) { if (m_Doc != null && m_Node != null) { bool bExists = false; string wstrValue = null; GetAttributeAt(m_strAttrName, out wstrValue, out bExists); if (false == bExists) { XmlAttributeCollection attrList = m_Node.Attributes; XmlAttribute pAttr = m_Doc.CreateAttribute(m_strAttrName, namespaceUri); if (attrList != null && pAttr != null) { attrList.SetNamedItem(pAttr); } return(true); } } return(false); }
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); }