C#自动化IO/XML作业
PS:这是我们公司自动化测试留的一个作业,虽然我不是自动化的,但是也做了一下。
这个也是我根据自动化部门的那次作业自己分析写的,没有写打log的过程,细化的时候再判断再分析吧,主要目的是学习C#。本次的内容是一个窗体程序:遍历指定目录下的文件;将文件信息存入XML中;将XML中的路径结构还原到指定目录下;如果扩展写的话还可以进行数据的备份和还原。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Blank
}
private void BackUp_Click(object sender, EventArgs e)
{
string path = CheckOutPath.Text.ToString();
//Create XML
CreateXML();
GetAllFiles(path);
}
//Get all files under the path
private void GetAllFiles(string path)
{
DirectoryInfo dir = new DirectoryInfo(@path);
FileInfo[] files = dir.GetFiles();
//Store files into XML
StoreIntoXML(dir.Parent.ToString(), files);
DirectoryInfo[] subDirs = dir.GetDirectories();
//Store subdirs into XML
StoreIntoXML(dir.Parent.ToString(),subDirs);
foreach (DirectoryInfo subDir in subDirs) {
string subPath = subDir.FullName;
GetAllFiles(subPath);
}
}
//Create the XML under the XMLForBackUpPath
private void CreateXML()
{
string XMLPath = XMLForBackUpPath.Text.ToString();
XmlDocument xml = new XmlDocument();
xml.LoadXml("<XML></XML>");
string filePath = Path.Combine(XMLPath,"BackUp.XML");
xml.Save(@filePath);
}
//Store subdirs into XML
private void StoreIntoXML(string parentDir,DirectoryInfo[] subDirs)
{
//Load the XML
string XMLPath = XMLForBackUpPath.Text.ToString();
string filePath = Path.Combine(XMLPath, "BackUp.XML");
XmlDocument xml = new XmlDocument();
xml.Load(@filePath);
//Append the child node to parentDir if possible
foreach (DirectoryInfo subDir in subDirs)
{
XmlElement subNode = xml.CreateElement("FolderNode");
xml.DocumentElement.AppendChild(subNode);
subNode.SetAttribute("type", "folder");
subNode.SetAttribute("path", subDir.FullName.ToString());
subNode.SetAttribute("name", subDir.ToString());
subNode.SetAttribute("parent", parentDir);
}
xml.Save(@filePath);
}
//Store files into XML
private void StoreIntoXML(string parentDir,FileInfo[] files)
{
//Load the XML
string XMLPath = XMLForBackUpPath.Text.ToString();
string filePath = Path.Combine(XMLPath, "BackUp.XML");
XmlDocument xml = new XmlDocument();
xml.Load(@filePath);
//Append the child node to parentDir if possible
foreach (FileInfo file in files)
{
XmlElement subNode = xml.CreateElement("FileNode");
xml.DocumentElement.AppendChild(subNode);
subNode.SetAttribute("type", "file");
subNode.SetAttribute("name", file.ToString());
subNode.SetAttribute("path", file.FullName.ToString());
subNode.SetAttribute("parent",parentDir);
}
xml.Save(@filePath);
}
private void Restore_Click(object sender, EventArgs e)
{
//Restore
string RestorePath=XMLForRestorePath.Text.ToString();
RestoreSolution(RestorePath);
}
//Restore the file system structure from the XML
private void RestoreSolution(string path)
{
XmlDocument XMLForRestore = new XmlDocument();
XMLForRestore.Load(@path);
XmlNodeList nodeLists = XMLForRestore.DocumentElement.ChildNodes;
foreach(XmlElement ele in nodeLists)
{
if (ele.GetAttribute("type") == "folder")
{
if (!System.IO.Directory.Exists(@ele.GetAttribute("path")))
{
Directory.CreateDirectory(@ele.GetAttribute("path"));
}
}
}
}
}
}
C#自动化IO/XML作业的更多相关文章
- IO流作业
IO流作业 一. 填空题 Java IO流可以分为 字节流 和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道 ...
- 2017.12.20 Java中的 IO/XML学习总结 File类详细
IO / XML 一.File类 1.定义/概念 Java是面向对象的语言,要想把数据存到文件中,就必须要有一个对象表示这个文件.File类的作用就是代表一个特定的文件或目录,并提供了若干方法对这些文 ...
- python接口自动化-发xml格式post请求
前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...
- javaee IO流作业02
package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fil ...
- javaee IO流作业
package Zy; import java.io.Serializable; public class Student implements Serializable{ private stati ...
- 老男孩python自动化运维作业2
拿到要求真不知道怎么写,不能还要写个商城页面吧: 最后还是用了input()模拟用户操作吧- -!不就操作个字典吗(字典模拟商品数据). python版本: >>>import sy ...
- 老男孩python自动化运维作业1
#!/usr/bin/env pthon #字典操作三级菜单 “b”返回上一级菜单,“q”退出. menu={"BJ":{"cp":{1:1,2:2,3:3}, ...
- 简单Java类与XML之间的转换
需要的jar包:xmlpull_1_0_5.jar,xstream-1.4.1.jar) 1.工具类XstreamUtil package com.learn.util; import com.tho ...
- JAVA对象和XML文档、原来他们之间还有这一出
最近项目开发中遇到一个问题,访问接口不再通过url地址请求的方式,而是 通过socket发送xml格式的报文到指定服务器来进行信息的统一认证.. 因此组装xml格式的报文字符串以及解析服务器返回的xm ...
随机推荐
- Smarty模板
Smarty模板 是做什么用的?? 是将前端的显示和后台的逻辑进行分离,就相当于把前台显示的页面和后台要实现的某些功能的逻辑给分离出来了,分离在两个文件里,也就是说,前端只负责显示,后端只负责逻辑操作 ...
- javascript浏览器对象
window对象 1.window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JS全局对象.函数以及变量均自动成为window对象的成员 全局变量是window对象的 ...
- TP隐藏入口
我们知道,在thinkphp的案例中有一个.htaccess文件,里面配置了URL的一些重写规则,如: <IfModule mod_rewrite.c> RewriteEngine on ...
- php操作oracle的方法类集全
在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...
- [v]Debian类系统的有效国内源
源文件的位置 /etc/apt/sources.list 因为测试需要,装完Debian7 后,更新为163的源,但是后来装软件时,一些软件依赖包还是装不上.后来把163源稍加改动,就好用了.163源 ...
- 「LAMP」在ubuntu及其衍生版上 安装LAMP
在Ubuntu上安装LAMP 此种方法在Linux Mint 13/14/15/16/17.Ubuntu 12.10(Quantal Quetzal)和Ubuntu 13.04 Raring Ring ...
- 弹出层和ajax数据交互
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- SSAS计算列如果是中文名称时,必须要在名字外加中括号
在SSAS中建计算列的时候,如果你给计算列起的是中文名字,一定记住要在名字外加中括号,比如下面这个例子中我们建了一个叫 客服流失数 的计算列 下面图中没有在计算列名称上加中括号这是错误的,因为使用中文 ...
- 【GDI+】一些规则多边形分离的问题
在近期的工作中,需要做一样工作:将一些有规则的图形,进行适当的分离,以达到不重叠的问题. 首先组成图形的点都可以是按照逆时针排好序的. 规则的图形可以大致分为三类: A :两个点组成的线 或者 四个点 ...
- 读 《JavaScript: The Good Parts》 有感
提炼出一门语言或技术的 Good Parts, 使用该子集去构造健壮稳固的应用. 我们总是倾向于去学习和使用所有的语言特性,好像凡是新的,凡是提供了的, 就有必要去使用: 这本书告诉我们, 要有选择性 ...