app教程网 综合百科 xml文档是干嘛的(xml文档的作用)

xml文档是干嘛的(xml文档的作用)

已知如下的XML文档(bookstore.xml ):

?xml version='1.0' encoding='gb2312'?bookstorebook genre='fantasy' ISBN='2-3631-4'titleOberons Legacy/titleauthorCorets, Eva/authorprice5.95/price/book/bookstore

1.将图书节点插入书店节点:

XML文档XML doc=new XML document();xmlDoc .加载('书店。XML’);XmlNode root=xmlDoc .SelectSingleNode('书店');//查找bookstoreXmlElement xe1=xmlDoc .CreateElement(' book ');//创建一个书节点xe1 .SetAttribute('genre ''李赞红');//配置该节点类型属性xe1 .SetAttribute('ISBN '' 2-3631-4 ');//配置该节点国际标准图书编号属性

XmlElement xesub1=xmlDoc .CreateElement(' title ');xesub1 .InnerText='CS从入门到精通;//配置文本节点xe1 .AppendChild(xe sub 1);//添加到书节点中XmlElement xesub2=xmlDoc .CreateElement(“作者”);xesub2 .InnerText='候捷;xe1 .AppendChild(xe sub 2);XmlElement xesub3=xmlDoc .CreateElement(' price ');xesub3 .InnerText=' 58.3xe1 .AppendChild(xe sub 3);

Root. AppendChild(xe1); //xmlDoc added to the bookstore node. Save ('bookstore.xml');

//===========结果是:

?可扩展标记语言版本='1.0 '编码='gb2312 '书店图书流派=' fantasy ' ISBN=' 2-3631-4 ' title ebrons Legacy/title authorcorets,Eva/author price 5.95/price/book图书流派='李赞红ISBN='2-3631-4 '标题从入门到精通/标题作者候捷/作者价格58.3/价格/书/书店

2.修改节点:将根属性值为“李赞宏”的节点的根值改为“更新李赞宏”,并将该节点的子节点改为author。

的文字改为“雅声”。

XmlNodeList nodeList=xmlDoc。 SelectSingleNode ('Bookstore'). Child node; //Get any subsections of the bookstore node.

point foreach(nodeList中的XmlNode xn)//遍历任意子节点{ XML element xe=(XML element)xn;//将子节点类型转换为XmlElement类型if (xe。getattribute ('genre')='李赞宏')//如果genre属性的值为'李赞宏' {xe。setattribute('流派''更新李赞宏');//将此属性修改为“更新李赞宏”

XmlNodeList nls=xe。子节点;//继续获取xe子节点的任意子节点foreach(NLS的xmlnode xn1)//Traverse { XML element xe2=(XML element)xn1;//转换类型if(xe2。Name=='author')//If {xe2。找到InnerText=' Yasheng '//修改break//随便找出口} } break}}

xmlDoc .保存(‘书店。XML’);//保存。

//=================

最后的结果是:

?可扩展标记语言版本='1.0 '编码='gb2312 '书店图书流派='奇幻' ISBN=' 2-3631-4 '书名ebrons Legacy/书名authorcorets,Eva/作者价格5.95/price/book图书流派='更新李赞红ISBN='2-3631-4 '标题从入门到精通/标题作者亚胜/作者价格58.3/价格/书/书店

3 ,删除图书类型='奇幻' ISBN='2-3631-4'Der Knoten性别属性,删除图书类型='更新李赞红」

ISBN='2-3631-4 '节点。

XmlNodeList xnl=xmlDoc.SelectSingleNode('bookstore').ChildNodes;

foreach(XmlNode xn in xnl){XmlElement xe=(XmlElement)xn;

如果(xe .get属性(' genre ')==' fantasy '){ xe .移除属性(“流派”);//删除类型属性}else if(xe .GetAttribute('genre')=='更新李赞红){xe .删除全部();//删除该节点的全部内容}}xmlDoc .保存(‘书店。XML’);

//====================

最后的结果是:

?xml version='1.0' encoding='gb2312'?bookstorebook ISBN='2-3631-4'titleOberons Legacy/titleauthorCorets, Eva/authorprice5.95/price/bookbook/book/bookstore

4.显示任何数据。

XmlNode xn=xmlDoc.SelectSingleNode('bookstore');

XmlNodeList xnl=xn.ChildNodes;

foreach(xnl中的XmlNode xnf){ XML element xe=(XML element)xnf;控制台WriteLine(xe .获取属性(“流派”);//显示属性值控制台WriteLine(xe .get属性(" ISBN ");

XmlNodeList xnf1=xe .子节点;foreach(xn f1中的XmlNode xn2){控制台.WriteLine(xn2 .InnerText);//显示子节点点文本}}

public static string DataToXml(string _ip,string _xmlType,bool _issavexml,string

_contenttype,string _message,string _sendtime,string _toip) { //return null; DataParser dp=new DataParser(); dp.Message=_message; dp.IP=_ip; dp.XmlType=_xmlType; dp.isSaveXml=_issavexml; dp.ContentType=_contenttype; dp.Sendtime=_sendtime; dp.Toip=_toip; XmlDocument doc=new XmlDocument(); XmlDeclaration newDec=doc.CreateXmlDeclaration('1.0',null,null); doc.AppendChild(newDec); XmlElement newRoot=doc.CreateElement('Requests'); doc.AppendChild(newRoot); XmlElement newtitle=doc.CreateElement('Request'); newtitle.SetAttribute('time', dp.Sendtime); newRoot.AppendChild(newtitle); XmlElement from=doc.CreateElement('from'); from.SetAttribute('ip', dp.IP); from.SetAttribute('type', dp.XmlType); from.SetAttribute('ctntype', dp.ContentType);

本文来自网络,不代表本站立场,转载请注明出处:https: