/// <summary> /// 设置一个对象(设置一个节点,保存对象的所有属性) /// </summary> /// <param name="name"></param> /// <param name="value"></param> public void SetNodeObject(object obj) { //检查当前节点是否被初始化 CheckCurrentNode(); //创建一个节点元素 XmlElement xd = CreateElement(obj.ToString()); ////设置元素的文本 //xd.InnerText = value; //将创建的元素追加到当前节点中 CurrentNode.AppendChild(xd); //初始化一个对象创建者 ObjectCreator oc = new ObjectCreator(obj); //获取对象属性集合 IDictionary <string, string> args = oc.GetAttributes(); //遍历属性集合,添加子节点 foreach (var arg in args) { SetChildNodeInnerText(arg.Key, arg.Value); } SaveFile(); }
/// <summary> /// 在当前节点下添加一个节点元素 /// </summary> /// <param name="name">元素名称</param> /// <param name="value">文本内容</param> private void AppendElement(string name, string value) { //创建一个节点元素 XmlElement xd = CreateElement(name); //设置元素的文本 xd.InnerText = value; //将创建的元素追加到当前节点中 CurrentNode.AppendChild(xd); }
public XmlNode CreateComment(string comment) { XmlNode commentNode = Document.CreateComment(comment); if (CurrentNode != null) { CurrentNode.AppendChild(commentNode); } else { Document.DocumentElement.AppendChild(commentNode); } return(commentNode); }
public XmlNode AddFirstChild(XmlNode newChildNode) { if (CurrentNode == null) { CurrentNode = Document.DocumentElement; } if (Document.DocumentElement == null) { throw new GlException("Xml document must have document element node"); } if (CurrentNode.FirstChild == null) { return(CurrentNode.AppendChild(newChildNode)); } else { return(CurrentNode.InsertBefore(newChildNode, CurrentNode.FirstChild)); } }
/// <summary> /// In the body state - no doctypes and declarations allowed. /// </summary> /// <param name="token">The consumed token.</param> void InMisc(XmlToken token) { switch (token.Type) { case XmlTokenType.Comment: { var tok = (XmlCommentToken)token; var com = doc.CreateComment(tok.Data); CurrentNode.AppendChild(com); break; } case XmlTokenType.ProcessingInstruction: { var tok = (XmlPIToken)token; var pi = doc.CreateProcessingInstruction(tok.Target, tok.Content); CurrentNode.AppendChild(pi); break; } case XmlTokenType.StartTag: { insert = XmlTreeMode.Body; InBody(token); break; } default: { if (!token.IsIgnorable) { throw Errors.Xml(ErrorCode.XmlMissingRoot); } break; } } }
/// <summary> /// In the body state - no doctypes and declarations allowed. /// </summary> /// <param name="token">The consumed token.</param> void InBody(XmlToken token) { switch (token.Type) { case XmlTokenType.StartTag: { var tok = (XmlTagToken)token; var tag = doc.CreateElement(tok.Name); CurrentNode.AppendChild(tag); if (!tok.IsSelfClosing) { open.Add(tag); } else if (open.Count == 0) { insert = XmlTreeMode.After; } for (int i = 0; i < tok.Attributes.Count; i++) { tag.SetAttribute(tok.Attributes[i].Key, tok.Attributes[i].Value.Trim()); } break; } case XmlTokenType.EndTag: { var tok = (XmlTagToken)token; if (CurrentNode.NodeName != tok.Name) { throw Errors.Xml(ErrorCode.TagClosingMismatch); } open.RemoveAt(open.Count - 1); if (open.Count == 0) { insert = XmlTreeMode.After; } break; } case XmlTokenType.ProcessingInstruction: case XmlTokenType.Comment: { InMisc(token); break; } case XmlTokenType.Entity: { var tok = (XmlEntityToken)token; var str = tokenizer.GetEntity(tok); CurrentNode.AppendText(str); break; } case XmlTokenType.CData: { var tok = (XmlCDataToken)token; CurrentNode.AppendText(tok.Data); break; } case XmlTokenType.Character: { var tok = (XmlCharacterToken)token; CurrentNode.AppendText(tok.Data); break; } case XmlTokenType.EOF: { throw Errors.Xml(ErrorCode.EOF); } case XmlTokenType.DOCTYPE: { throw Errors.Xml(ErrorCode.XmlDoctypeAfterContent); } case XmlTokenType.Declaration: { throw Errors.Xml(ErrorCode.XmlDeclarationMisplaced); } } }
void InBody(XmlToken token) { switch (token.Type) { case XmlTokenType.StartTag: { var tok = (XmlTagToken)token; var tag = doc.CreateElement(tok.Name); if (!tok.IsSelfClosing) { open.Add(tag); } CurrentNode.AppendChild(tag); for (int i = 0; i < tok.Attributes.Count; i++) { tag.SetAttribute(tok.Attributes[i].Key, tok.Attributes[i].Value); } break; } case XmlTokenType.EndTag: { if (open.Count == 0) { throw new ArgumentException("Unexpected end-tag (no current element)."); } var tok = (XmlTagToken)token; if (CurrentNode.NodeName != tok.Name) { throw new ArgumentException("Mismatched end-tag."); } open.RemoveAt(open.Count - 1); break; } case XmlTokenType.Comment: { var tok = (XmlCommentToken)token; var com = doc.CreateComment(tok.Data); CurrentNode.AppendChild(com); break; } case XmlTokenType.ProcessingInstruction: { var tok = (XmlPIToken)token; var pi = doc.CreateProcessingInstruction(tok.Target, tok.Content); CurrentNode.AppendChild(pi); break; } case XmlTokenType.Character: { //Append character to node break; } case XmlTokenType.EOF: { if (open.Count != 0) { RaiseErrorOccurred(ErrorCode.EOF); open.RemoveRange(0, open.Count); } break; } case XmlTokenType.DOCTYPE: { RaiseErrorOccurred(ErrorCode.DoctypeUnexpected); break; } case XmlTokenType.Declaration: { RaiseErrorOccurred(ErrorCode.UndefinedMarkupDeclaration); break; } } }