XML文件
<?xml version="1.0" encoding="utf-8"?>
<Simnumber>
<Tlinkman Ter_ID="10011000030000000011" TSim="460021238303270">已回收,暂未安装</Tlinkman>
<Tlinkman Ter_ID="10011000200000000001" TSim="555"></Tlinkman>
<Tlinkman Ter_ID="10011000010000000001" TSim="460021238303270">市场运营中心</Tlinkman>
</Simnumber>
1.创建XML |
//创建XML
private void btn_Add_Click(object sender, EventArgs e)
{
if (!File.Exists(path))
{
XElement root = new XElement("Simnumber",
new XElement("Tlinkman",
new XAttribute("Ter_ID", ""),
new XAttribute("TSim", "")
)
);
root.Save(path);
MessageBox.Show("成功!", "消息提示");
}
else
{
MessageBox.Show("xml已存在!", "消息提示");
}
}
2.删除XML |
//删除XML
private void btn_Delete_Click(object sender, EventArgs e)
{
if (!File.Exists(path))
{
MessageBox.Show("xml还未创建,请创建!", "消息提示");
}
if (File.Exists(path))
{
FileInfo file = new FileInfo(path);
if (MessageBox.Show("是否确认删除!?", "消息提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
file.Delete();
}
}
}
3.查询 |
//查询TSim && Tlinkman
private void btn_id_Search_Click(object sender, EventArgs e)
{
XElement root = XElement.Load(path);
var address =
from el in root.Elements("Tlinkman")
where (string)el.Attribute("Ter_ID") == txt_ID.Text
select el;
//判断查询结果
if (address.Count() == 0)
{
txt_error.Text = "-_-。:您查询的结果不存在.";
txt_Sim.Text = "无结果";
txt_Tlink.Text = "无结果";
}
if (address.Count() != 0)
{
foreach (XElement el in address)
{
txt_Tlink.Text = el.Value;
txt_Sim.Text = el.Attribute("TSim").Value;
txt_error.Text = txt_ID.Text + ",\n" +
" #^_^# :查询成功!";
}
}
}
4.新增 |
第1次新增
第2次新增
XML:
<?xml version="1.0" encoding="utf-8"?>
<Simnumber>
<Tlinkman Ter_ID="10011000030000000011" TSim="460021238303270">已回收,暂未安装</Tlinkman>
<Tlinkman Ter_ID="10011000200000000001" TSim="555"></Tlinkman>
<Tlinkman Ter_ID="10011000010000000001" TSim="460021238303270">市场运营中心</Tlinkman>
<Tlinkman Ter_ID="122123" TSim="333333">333333</Tlinkman>
</Simnumber>
代码文档:
//新增TSim && Tlinkman
private void btn_ADD_TSim_Click(object sender, EventArgs e)
{
if (txt_ID.Text != "" && txt_Tlink.Text != "" && txt_Sim.Text != "")
{
XDocument xdoc = XDocument.Load(path);
XElement root = xdoc.Root;
var address =
from el in root.Elements("Tlinkman")
where (string)el.Attribute("Ter_ID") == txt_ID.Text
select el;
if (address.Count() == 0)
{
root.Add(
new XElement("Tlinkman", txt_Tlink.Text,
new XAttribute("Ter_ID", txt_ID.Text),
new XAttribute("TSim", txt_Sim.Text)
)
);
root.Save(path);
txt_error.Text = "Ter_ID = " + txt_ID.Text + "添加成功!";
}
else
{
txt_error.Text = "Sorry,Ter_ID = " + txt_ID.Text + "已存在!不能重复创建。。";
}
}
else
{
txt_error.Text = "Ter_ID、TSim和Tlinkman不能为空!";
}
}
5.修改 |
XML
<?xml version="1.0" encoding="utf-8"?>
<Simnumber>
<Tlinkman Ter_ID="10011000030000000011" TSim="460021238303270">已回收,暂未安装</Tlinkman>
<Tlinkman Ter_ID="10011000200000000001" TSim="555"></Tlinkman>
<Tlinkman Ter_ID="10011000010000000001" TSim="460021238303270">市场运营中心</Tlinkman>
<Tlinkman Ter_ID="122123" TSim="qqqqq">qqqqq</Tlinkman>
</Simnumber>
代码文档:
//修改TSim && Tlinkman
private void button1_Click(object sender, EventArgs e)
{
if (txt_ID.Text != "" && txt_Tlink.Text != "" && txt_Sim.Text != "")
{
XDocument xdoc = XDocument.Load(path);
XElement root = xdoc.Root;
var p = from pper in root.Elements("Tlinkman")
where pper.Attribute("Ter_ID").Value == txt_ID.Text
select pper;
if (p.Count() != 0)
{
foreach (XElement xe in p)
{
xe.Value = txt_Tlink.Text;
xe.SetAttributeValue("TSim", txt_Sim.Text);
}
root.Save(path);
txt_error.Text = "Ter_ID = ‘" + txt_ID.Text + "’修改成功!";
}
else
{
txt_error.Text = "Ter_ID = ‘" + txt_ID.Text + "’不存在,\r\n请先查询再修改!";
}
}
else
{
txt_error.Text = "Ter_ID、TSim和Tlinkman不能为空!";
}
}
6.删除 |
(1)Ter_ID不存在时,
(2)Ter_ID存在时,
<?xml version="1.0" encoding="utf-8"?>
<Simnumber>
<Tlinkman Ter_ID="10011000030000000011" TSim="460021238303270">已回收,暂未安装</Tlinkman>
<Tlinkman Ter_ID="10011000200000000001" TSim="555"></Tlinkman>
<Tlinkman Ter_ID="10011000010000000001" TSim="460021238303270">市场运营中心</Tlinkman>
</Simnumber>
(3)Ter_ID=12213已删除
//删除TSim && Tlinkman
private void btn_delete_sim_Click(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load(path);
XElement root = xdoc.Root;
var p = from pper in root.Elements("Tlinkman")
where pper.Attribute("Ter_ID").Value == txt_ID.Text
select pper;
if (p.Count() != 0)
{
p.Remove();
txt_error.Text = txt_ID.Text + "删除成功!";
root.Save(path);
}
else
{
txt_error.Text = "不能删除!查询无结果。。";
}
}
7.开启(同步数据库。。未完善。。) |
public void Str()
{
XDocument xdoc = XDocument.Load(path);
XElement root = xdoc.Root;
foreach (XElement item in root.Elements("Tlinkman"))
{
string sim = item.Attribute("TSim").Value;
string terid = item.Attribute("Ter_ID").Value;
string tlinkman = item.Value;
//Console.WriteLine("{0},{1},{2}", tlinkman, terid, sim);
Ter_Base_Model model = new Ter_Base_Model();
model.Ter_ID = terid;
model.TSim = sim;
model.Tlinkman = tlinkman;
Ter_Base_Info bll = new Ter_Base_Info();
bll.Update(model);
}
}
//开启
private void btn_Start_Click(object sender, EventArgs e)
{
Str();
}
其它代码就不贴出来了。