UI Automation-creat XML(二)
来源:互联网 发布:手机淘宝地址在哪看 编辑:程序博客网 时间:2024/04/30 11:20
首先需要添加的命名空间是using Sytem.Xml;然后定义几个公共对象:
XmlDocument xmldoc; XmlNode xmlnode; XmlElement xmlelem;
创建到服务器同名录下的xml文件:
方法一:
xmldoc = new XmlDocument ( ) ;
//加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
xmldoc.AppendChild ( xmldecl);
//加入一个根元素
xmlelem = xmldoc.CreateElement ( "" , "Employees" , "" ) ;
xmldoc.AppendChild ( xmlelem ) ;
//加入另外一个元素
for(int i=1;i<3;i++)
{
XmlNode root=xmldoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=xmldoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElement xesub1=xmldoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild (xesub1);//添加到<Node>节点中
XmlElement xesub2=xmldoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmldoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>节点中
}
//保存创建好的XML文档
xmldoc. Save ( Server.MapPath("data.xml") ) ;
/////////////////////////////////////////////////////////////////////////////
/////////
结果:在同名目录下生成了名为data.xml的文件,内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>
方法二:
public static void WriteXML(string filename)
{
try
{
// 创建XmlTextWriter类的实例对象
XmlTextWriter xmlWriter = new XmlTextWriter(filename, null);
xmlWriter.Formatting = Formatting.Indented;
// 开始写过程,调用WriteStartDocument方法
xmlWriter.WriteStartDocument();
// 写入说明
xmlWriter.WriteComment("New contacts and groups from XML");
//创建节点
xmlWriter.WriteStartElement("Contacts ");
xmlWriter.WriteStartElement("Contacts");
xmlWriter.WriteAttributeString("Attribute", "Required");
xmlWriter.WriteElementString("Email", "XXX@126.com");
xmlWriter.WriteElementString("Email", "XXX@163.com");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Groups");
xmlWriter.WriteAttributeString("Attribute", "Required");
xmlWriter.WriteElementString("Group_Name", "Family");
xmlWriter.WriteElementString("Group_Name", "Friends");
xmlWriter.WriteElementString("Group_Name", "colleague");
xmlWriter.WriteElementString("Group_Name", "i");
xmlWriter.WriteElementString("Group_Name", "_");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
// 写文档结束,调用WriteEndDocument方法
xmlWriter.WriteEndDocument();
// 关闭textWriter
xmlWriter.Close();
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
}
- UI Automation-creat XML(二)
- UI Automation-XML(二)
- UI Automation-InvokePattern
- UI Automation-ValuePattern
- UI Automation-TogglePattern
- UI Automation-WindowPattern
- UI Automation-GridPattern
- UI Automation-TablePattern
- UI Automation-RangeValuePattern
- UI Automation-TransformPattern
- UI Automation-RangeValuePattern
- UI Automation-TablePattern
- UI Automation-TransformPattern
- UI Automation-GridPattern
- UI Automation-WindowPattern
- UI Automation-TogglePattern
- UI Automation-ValuePattern
- UI Automation-InvokePattern
- PHP实例:用PHP简单实现多条件查询
- Linux LCD 10分钟黑屏,十分钟黑屏,LCD自动关闭,LCD黑屏 问题
- 关于C++中const的应用
- SELECT DISTINCT 语句
- INetFwAuthorizedApplication和INetFwOpenPort用法
- UI Automation-creat XML(二)
- excel导出总结
- 【c#开发】终于理解为什么说InputPanel是全局的了!
- div最小值
- 开源正则库及其使用
- JAR破解原理
- JavaScript控制checkbox全选,以及获取checkbox文本
- C# 控制本地计算机注销,重启,关机命令
- Runtime Error---Description: An application error occurred on the server....