![]() | CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
CYQ.Data.Xml XmlHelper 帮助类的使用帮助
Guide |
|
|
| #TopicOwner |
PS:V5版本已变更名称为XHtmlAction类名。 一:XmlHelper 类的说明 使用此类,可以轻松操作Xml/Html。
二:XmlHelper 使用说明: 1:实例化: XmlHelper doc=new XmlHelper(...);
方法原型: public XmlHelper()//默认构造函数[操作无名称空间的Xml] public XmlHelper(bool forHtml)//参数为true时表示操作Html,为false时表示操作xml public XmlHelper(string nameSpaceUrl)//操作名称空间的Xml 2:Xml/Html加载: 方法原型: public void LoadXml(string xml)//从字符串加载 public bool Load(string absFilePath)//从文件路径加载 public bool Load(string absFilePath, XmlCacheLevel level)//带设置xml/html缓存级别 示例代码: XmlHelper doc = new XmlHelper (true);//操作html
if (doc.Load(Server.MapPath("html/index.html"), XmlCacheLevel.Hour)) { //加载html成功,并缓存html 1小时 } 2:单结点查询: 方法原型: public XmlNode GetByID(string id)//根据ID查找结点
public XmlNode GetByID(string id, XmlNode parentNode)//在parentNode节点下根据ID找结点 public XmlNode GetByName(string name)//根据Name查找结点 public XmlNode GetByName(string name, XmlNode parentNode)) public XmlNode Get(string tag, string attr, string value, XmlNode parentNode)//根据标签,属性及属性值和是否带父节点查找一个结点。 示例: xml示例节点: <p id="pID">这里有pID</p><div id="divID" ><p id="pID">pID又来一个</p></div> XmlNode node = doc.GetByID("pID", doc.GetByID("divID"));//获取里面的ID节点 3:列表结点查询: 方法原型: public XmlNodeList GetList(string tag)
public XmlNodeList GetList(string tag, XmlNode parentNode) public XmlNodeList GetList(string tag, string attr) public XmlNodeList GetList(string tag, string attr, XmlNode parentNode) public XmlNodeList GetList(string tag, string attr, string value) public XmlNodeList GetList(string tag, string attr, string value, XmlNode parentNode) 示例: xml示例节点: <p id="pID">这里有pID</p><div id="divID" ><p id="pID">pID又来一个</p></div> XmlNodeList nodeList = doc.GetList("p","id","pid");//获取所有p的节点 4:创建结点: 方法原型: public XmlNode CreateNode(string tag, string text, params string[] attrAndValue) public void CreateNodeTo(XmlNode parentNode, string tag, string text, params string[] attrAndValue) 示例: xml示例节点: 一开始节点:<div id="divID"></div> 代码创建:doc.CreateNodeTo(doc.GetByID("divID"), "p", "pID又来一个", "id", "pID"); 创建后变成:<div id="divID" ><p id="pID">pID又来一个</p></div> 5:添加结点: 方法原型: public void AppendNode(XmlNode parentNode, XmlNode childNode) public void AppendNode(XmlNode parentNode, XmlNode childNode, int position)//position为将childNode放在parentNode的第N个子节点之后 6:删除结点: 方法原型: public void Clear(XmlNode node)//保留节点,只清除内容和属性
public void Remove(XmlNode node)//删除节点 public void Remove(string id)//根据节点ID删除节点 public void RemoveChild(string id, int index)//移除某ID节点下的第N个子节点 public void RemoveChild(XmlNode node, int index)//移除某节点下的第N个子节点 public void RemoveAttrList(params string[] attrNames)//移除整个文档的指定属性 7:结点交互|替换|插入[前后] 方法原型: public void InterChange(XmlNode xNodeFirst, XmlNode xNodeLast)//两节点互换位置 public void ReplaceNode(XmlNode newNode, XmlNode oldNode)//节点替换 public void InsertAfter(XmlNode newNode, XmlNode refNode)//将newNode插入到refNode之后 public void InsertBefore(XmlNode newNode, XmlNode refNode)//将newNode插入到refNode之前 8:节点判断: 方法原型: public bool Contains(string id)//是否存在某ID节点 public bool Contains(string id, XmlNode parentNode)//在parentNode下是否存在某ID节点 9:属性操作 方法原型: public bool HasAttr(string nodeID, string attrName)//某节点是否存在某属性 public bool HasAttr(XmlNode node, string attrName) public string GetAttrValue(string nodeID, string attrName, params string[] defaultValue)//获取某节点的属性值,并允许设置默认值 public string GetAttrValue(XmlNode node, string attrName, params string[] defaultValue) public void RemoveAttr(string nodeID, params string[] attrNames)//移除节点指定属性 public void RemoveAttr(XmlNode node, params string[] attrNames) 10:保存为xml/html功能 方法原型: public void Save()//保存并替换原始加载文件 public void Save(string fileName)//保存到指定的文件路径 三:与CYQ.Data.Table的交互功能 1:原生的赋值操作 方法原型: public void Set(string id, string value)//为ID节点的InnerXml属性赋值
public void Set(string id, SetType setType, params string[] values)//为ID节点的setType[不同属性]赋值,如果是A链接,可赋多个值。 public void Set(XmlNode node, SetType setType, params string[] values) 示例: 原始:<a id="aID">这是一个原始A链接</id> doc.Set("aID", SetType.A,"这是A链接","http://www.cyqdata.cn","_blank"); 结果:<a id="aID" href=http://www.cyqdata.cn target="_blank">这是A链接</id> 2:与MDataRow的交互[ID=任意三个字母前缀+字段名称] 方法原型: public void LoadData(MDataRow row)//加载行数据
public void SetFor(string id)//将row的数据赋给ID节点的InnerXml属性 public void SetFor(string id, SetType setType)//将row的数据赋给ID节点的指定属性 public void SetFor(string id, SetType setType, params string[] values)//最后参数允许格式化值 示例: 原始<span id="labHits">点击</span> doc.SetFor("labHits", SetType.InnerText, ValueReplace.Source + "(" + ValueReplace.New + ")"); 结果:<span id="labhits">点击(12)</span> ValueReplace.Source:指原来节点属性的值 ValueReplace.New: 指数据库的值 3:与MDataTable的交互 方法原型: public void LoadData(MDataTable table)//加载表格 //SetForeach将ID节点复制并循环 示例一:SetForeach(string id, string text, params object[] formatValues) 原始标签:<select id="selClassID"></select>
doc.LoadData(table);//加载表格
doc.SetForeach("selClassID", "<option value="{0}" >{1}</option>", Class.ID, IsUserLang ? Class.Name : Class.PKey); 结果标签:<select id="selClassID"><option value="1">标识1</option>....</select>
这语句将循环出一个下拉列表。 示例二:SetForeach(string id, SetType setType, params object[] formatValues) 原始标签:<select id="selClassID"><option value="{0}" >{1}</option></select> doc.SetForeach("selClassID", SetType.InnerXml,, Class.ID, IsUserLang ? Class.Name : Class.PKey); 结果标签:和上面一样。 示例三:循环格式化: doc.OnForeach += new XmlHelper.SetForeachEventHandler(Document_OnForeach); string Document_OnForeach(string text, object[] values, int row) //text为:<option value="{0}" >{1}</option> values[1]=“标识2”; return text; 结果标签:<select id="selClassID"><option value="2">标识2</option>....</select> ![]() |
游客[注册][124.206.31.*]2013/6/11 17:18:15 | #1 | |
![]() | 不错 |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.