public static XmlElement CreateAndAddElement(XmlNode parentNode, string childName) { XmlElement node; if (parentNode.GetType() == typeof(XmlDocument)) node = ((XmlDocument)parentNode).CreateElement(childName); else node = parentNode.OwnerDocument.CreateElement(childName); parentNode.AppendChild(node); return node; }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="name"></param> /// <param name="canNull"></param> /// <returns></returns> public static string GetAttribute(XmlNode node, string name, bool canNull) { if (node == null) throw new ArgumentNullException("node"); XmlElement e = node as XmlElement; if (e == null) throw new ArgumentException("node must be XmlElement, but was " + node.GetType().Name); return GetAttribute(e, name, canNull); }
public static XmlAttribute CreateAndAddAttribute(XmlNode parentNode, string attributeName, string innerText) { XmlAttribute node; if (parentNode.GetType() == typeof(XmlDocument)) node = ((XmlDocument)parentNode).CreateAttribute(attributeName); else node = parentNode.OwnerDocument.CreateAttribute(attributeName); parentNode.Attributes.Append(node); if (innerText != null) node.InnerText = innerText; return node; }
public static XmlNode AppendElement( XmlNode node, string newElementName, string innerValue ) { XmlNode oNode ; if ( node.GetType() == typeof(XmlDocument) ) oNode = node.AppendChild( ((XmlDocument)node).CreateElement( newElementName ) ) ; else oNode = node.AppendChild( node.OwnerDocument.CreateElement( newElementName ) ) ; if ( innerValue != null ) oNode.AppendChild( node.OwnerDocument.CreateTextNode( innerValue ) ) ; return oNode ; }
public static XmlElement CreateAndAddElementWithValue(XmlNode parentNode, string childName, string val) { XmlElement node; XmlDocument owner = null; if (parentNode.GetType() == typeof(XmlDocument)) owner = (XmlDocument)parentNode; else owner = parentNode.OwnerDocument; node = owner.CreateElement(childName); parentNode.AppendChild(node); XmlAttribute attr = owner.CreateAttribute("value"); attr.InnerText = val; node.Attributes.Append(attr); return node; }
/// <summary> /// 创建一个XmlNode并添加到文档 /// </summary> /// <param name="clsParentNode">父节点</param> /// <param name="szNodeName">结点名称</param> /// <returns>XmlNode</returns> private static XmlNode CreateXmlNode(XmlNode clsParentNode, string szNodeName) { try { XmlDocument clsXmlDoc = null; if (clsParentNode.GetType() != typeof(XmlDocument)) clsXmlDoc = clsParentNode.OwnerDocument; else clsXmlDoc = clsParentNode as XmlDocument; XmlNode clsXmlNode = clsXmlDoc.CreateNode(XmlNodeType.Element, szNodeName, string.Empty); if (clsParentNode.GetType() == typeof(XmlDocument)) { clsXmlDoc.LastChild.AppendChild(clsXmlNode); } else { clsParentNode.AppendChild(clsXmlNode); } return clsXmlNode; } catch { return null; } }
private string IdentifyNode(XmlNode node) { if (node == null) return "<null>"; return String.Format(TypeConverterHelper.InvariantEnglishUS, "{0} ({1})", node.GetType().Name, node.Name); }
//FROM XML public void FromXml(XmlNode item) { Attributes.Clear(); Name = ""; Body = ""; IsCommentary = false; switch (item.GetType().ToString()) { case "System.Xml.XmlComment": IsCommentary = true; Name = "Commentary"; Body = item.InnerText; break; case "System.Xml.XmlElement": Name = item.Name; foreach (XmlAttribute att in item.Attributes) SetAttribute(att.Name, att.Value); if (!String.IsNullOrEmpty(item.InnerText)) Body = item.InnerText; break; } SourceXML = string.IsNullOrEmpty(SourceXML) ? item.OuterXml : SourceXML; }
/// <summary> /// Tests to see if it should add the field (IfData), then adds the field. /// </summary> /// <param name="path"></param> /// <param name="node"></param> /// <param name="reuseMap"></param> /// <param name="editor">Type of Contained Data</param> /// <param name="flid">Field ID</param> /// <param name="obj"></param> /// <param name="indent"></param> /// <param name="insPos"></param> /// <param name="fTestOnly"></param> /// <param name="fVisIfData">IfData</param> /// <param name="caller"></param> /// <returns>NodeTestResult, an enum showing if usable data is contained in the field</returns> private NodeTestResult AddSimpleNode(ArrayList path, XmlNode node, ObjSeqHashMap reuseMap, string editor, int flid, ICmObject obj, int indent, ref int insPos, bool fTestOnly, bool fVisIfData, XmlNode caller) { if (fVisIfData) // Contains the tests to see if usable data is inside the field (for all types of fields) { if (editor != null && editor == "custom") { System.Type typeFound; System.Reflection.MethodInfo mi = XmlUtils.GetStaticMethod(node, "assemblyPath", "class", "ShowSliceForVisibleIfData", out typeFound); if (mi != null) { object[] parameters = new object[2]; parameters[0] = (object)node; parameters[1] = (object)obj; object result = mi.Invoke(typeFound, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic, null, parameters, null); if (!(bool)result) return NodeTestResult.kntrNothing; } } else if (flid == 0 && editor != null && editor == "autocustom") { flid = SliceFactory.GetCustomFieldFlid(caller, m_cache.MetaDataCacheAccessor, obj); } if (flid != 0) { FieldType fieldType = m_cache.GetFieldType(flid); fieldType &= FieldType.kcptVirtualMask; // strip virtual bit. switch (fieldType) { default: // if we don't know how to check, make it visible. break; // These cases are a bit tricky. We're duplicating some information here about how the slices // interpret their ws parameter. Don't see how to avoid it, though, without creating the slices even if not needed. case FieldType.kcptMultiString: case FieldType.kcptMultiUnicode: case FieldType.kcptMultiBigString: case FieldType.kcptMultiBigUnicode: string ws = XmlUtils.GetOptionalAttributeValue(node, "ws", null); switch(ws) { case "vernacular": if (m_cache.MainCacheAccessor.get_MultiStringAlt(obj.Hvo, flid, m_cache.DefaultVernWs).Length == 0) return NodeTestResult.kntrNothing; break; case "analysis": if (m_cache.MainCacheAccessor.get_MultiStringAlt(obj.Hvo, flid, m_cache.DefaultAnalWs).Length == 0) return NodeTestResult.kntrNothing; break; default: if (editor == "jtview") { if (m_cache.MainCacheAccessor.get_MultiStringAlt(obj.Hvo, flid, m_cache.DefaultAnalWs).Length == 0) return NodeTestResult.kntrNothing; } // try one of the magic ones for multistring int wsMagic = LangProject.GetMagicWsIdFromName(ws); if (wsMagic == 0 && editor == "autocustom") { wsMagic = m_cache.MetaDataCacheAccessor.GetFieldWs((uint)flid); } if (wsMagic == 0 && editor != "autocustom") break; // not recognized, treat as visible ILgWritingSystem[] rgws = SIL.FieldWorks.Common.Widgets.LabeledMultiStringView.GetWritingSystemList(m_cache, wsMagic, false); bool anyNonEmpty = false; foreach (ILgWritingSystem wsInst in rgws) { if (m_cache.MainCacheAccessor.get_MultiStringAlt(obj.Hvo, flid, wsInst.Hvo).Length != 0) { anyNonEmpty = true; break; } } if (!anyNonEmpty) return NodeTestResult.kntrNothing; break; } break; case FieldType.kcptString: case FieldType.kcptBigString: if (m_cache.MainCacheAccessor.get_StringProp(obj.Hvo, flid).Length == 0) return NodeTestResult.kntrNothing; break; case FieldType.kcptUnicode: case FieldType.kcptBigUnicode: string val = m_cache.MainCacheAccessor.get_UnicodeProp(obj.Hvo, flid); if (val == null || val.Length == 0) return NodeTestResult.kntrNothing; break; // Usually, the header nodes for sequences and atomic object props // have no editor. But sometimes they may have a jtview summary // or the like. If an object-prop flid is specified, check it, // in case we want to suppress the whole header. case FieldType.kcptOwningAtom: case FieldType.kcptReferenceAtom: int hvoT = m_cache.MainCacheAccessor.get_ObjectProp(obj.Hvo, flid); if (hvoT == 0) return NodeTestResult.kntrNothing; int clid = m_cache.GetClassOfObject(hvoT); if (clid == (int)CellarModuleDefns.kclidStText) // if clid is an sttext clid { // Test if the StText has only one paragraph int cpara = m_cache.GetVectorSize(hvoT, (int)CellarModuleDefns.kflidStText_Paragraphs); if (cpara == 1) { // Tests if paragraph is empty int hvoPara = m_cache.GetVectorItem(hvoT, (int)CellarModuleDefns.kflidStText_Paragraphs, 0); if (hvoPara == 0) return NodeTestResult.kntrNothing; ITsString tss = m_cache.GetTsStringProperty(hvoPara, (int)CellarModuleDefns.kflidStTxtPara_Contents); if (tss == null || tss.Length == 0) return NodeTestResult.kntrNothing; } } break; case FieldType.kcptOwningCollection: case FieldType.kcptOwningSequence: case FieldType.kcptReferenceCollection: case FieldType.kcptReferenceSequence: if (m_cache.MainCacheAccessor.get_VecSize(obj.Hvo, flid) == 0) return NodeTestResult.kntrNothing; break; } } else if (editor == null) { // may be a summary node for a sequence or atomic node. Suppress it as well as the prop. XmlNode child = null; int cnodes = 0; foreach (XmlNode n in node.ChildNodes) { if (node.GetType() == typeof(XmlComment)) continue; cnodes++; if (cnodes > 1) break; child = n; } if (cnodes == 1) // exactly one non-comment child { int flidChild = GetFlidFromNode(child, obj); // If it's an obj or seq node and the property is empty, we'll show nothing. if (flidChild != 0 && child.Name == "seq" && m_cache.MainCacheAccessor.get_VecSize(obj.Hvo, flidChild) == 0) { return NodeTestResult.kntrNothing; } if (flidChild != 0 && child.Name == "obj" && m_cache.MainCacheAccessor.get_ObjectProp(obj.Hvo, flidChild) == 0) { return NodeTestResult.kntrNothing; } } } } if (fTestOnly) return NodeTestResult.kntrSomething; // slices always produce something. path.Add(node); Slice slice = GetMatchingSlice(path, reuseMap); if (slice == null) { slice = SliceFactory.Create(m_cache, editor, flid, node, obj, StringTbl, PersistenceProvder, m_mediator, caller); if (slice == null) { // One way this can happen in TestLangProj is with a part ref for a custom field that // has been deleted. return NodeTestResult.kntrNothing; } Debug.Assert(slice != null); // Set the label and abbreviation (in that order...abbr defaults to label if not given if (slice.Label == null) slice.Label = GetLabel(caller, node, obj, "label"); slice.Abbreviation = GetLabelAbbr(caller, node, obj, slice.Label, "abbr"); // Install new item at appropriate position and level. slice.Indent = indent; slice.Object = obj; slice.Cache = m_cache; slice.Mediator = m_mediator; // We need a copy since we continue to modify path, so make it as compact as possible. slice.Key = path.ToArray(); slice.ConfigurationNode = node; slice.CallerNode = caller; slice.OverrideBackColor(XmlUtils.GetOptionalAttributeValue(node, "backColor")); slice.ShowContextMenu += new TreeNodeEventHandler(this.OnShowContextMenu); slice.SmallImages = SmallImages; SetNodeWeight(node, slice); slice.FinishInit(); slice.Visible = false; // don't show it until we position and size it. InsertSliceAndRegisterWithContextHelp(insPos, slice, node); } else { EnsureValidIndexForReusedSlice(slice, insPos); } insPos++; slice.GenerateChildren(node, caller, obj, indent, ref insPos, path, reuseMap); path.RemoveAt(path.Count - 1); return NodeTestResult.kntrNothing; // arbitrary what we return if not testing (see first line of method.) }