欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

xml 文件操作

程序员文章站 2022-04-16 08:21:25
'XML添加 Public Sub Add(ID As String, RFSerialnumber As String, Mood As Integer) If reatch(RFSerialnumber) = 1 Then Return End If Dim xmlDoc As New XmlD ......
'xml添加
 
public sub add(id as string, rfserialnumber as string, mood as integer)
 
 
if reatch(rfserialnumber) = 1 then
return
end if
 
dim xmldoc as new xmldocument()
path = system.io.path.getfullpath("../../mood.xml")
xmldoc.load(path)
dim root as xmlnode = xmldoc.selectsinglenode("recard") '查找<bookstore>
dim xe1 as xmlelement = xmldoc.createelement("rfid") '创建一个<book>节点
xe1.setattribute("type", "ic卡") '设置该节点genre属性
'xe1.setattribute("id", ) '设置该节点isbn属性
dim xesub1 as xmlelement = xmldoc.createelement("id")
xesub1.innertext = id '设置文本节点
xe1.appendchild(xesub1) '添加到<book>节点中
dim xesub2 as xmlelement = xmldoc.createelement("rfserialnumber")
xesub2.innertext = rfserialnumber
xe1.appendchild(xesub2)
dim xesub3 as xmlelement = xmldoc.createelement("mood")
xesub3.innertext = mood
xe1.appendchild(xesub3)
root.appendchild(xe1) '添加到<bookstore>节点中
xmldoc.save(path)
 
end sub
public function read(rfserialnumber as string) as int32
path = system.io.path.getfullpath("../../mood.xml")
dim xmldoc as new xmldocument()
xmldoc.load(path)
dim xn as xmlnode
xn = xmldoc.selectsinglenode("recard")
dim xnl as xmlnodelist
xnl = xn.childnodes
dim xnc as xmlnode
xnc = xn.selectsinglenode("rfid")
dim moodid as integer
moodid = 0
for index = 0 to xnl.count - 1
if xnl(index).item("rfserialnumber").innertext = rfserialnumber then
moodid = convert.toint32(xnl(index).item("mood").innertext)
 
end if
 
next
return moodid
end function
''' <summary>
''' 查询
''' </summary>
''' <param name="rfserialnumber"></param>
''' <returns></returns>
public function reatch(rfserialnumber as string) as int32
 
dim xmldoc as new xmldocument()
path = system.io.path.getfullpath("../../mood.xml")
xmldoc.load(path)
dim xn as xmlnode
xn = xmldoc.selectsinglenode("recard")
dim xnl as xmlnodelist
xnl = xn.childnodes
dim xnc as xmlnode
xnc = xn.selectsinglenode("rfid")
dim num as integer
num = 0
for index = 0 to xnl.count - 1
if xnl(index).item("rfserialnumber").innertext = rfserialnumber then
num = 1
 
end if
 
next
return num
end function
 
 
private sub addpressure_visualizationxml(pressure_visualization as pressure_visualization)
dim path as string
dim xmldoc as new xmldocument()
path = system.io.path.getfullpath("../../../pressure_visualization.xml")
xmldoc.load(path)
dim root as xmlnode = xmldoc.selectsinglenode("pressure_visualizationconfig") '查找<bookstore>
dim xe1 as xmlelement = xmldoc.createelement("pressure_visualization") '创建一个<book>节点
xe1.setattribute("type", "ic卡") '设置该节点genre属性
'xe1.setattribute("id", ) '设置该节点isbn属性
dim xesub1 as xmlelement = xmldoc.createelement("t_device_address")
dim xesub2 as xmlelement = xmldoc.createelement("t_equipment_number")
dim xesub3 as xmlelement = xmldoc.createelement("t_equipment_state")
dim xesub4 as xmlelement = xmldoc.createelement("t_packing_pressure")
dim xesub5 as xmlelement = xmldoc.createelement("t_pressure_holding_time")
dim xesub6 as xmlelement = xmldoc.createelement("t_upper_limit_pressure")
dim xesub7 as xmlelement = xmldoc.createelement("t_lower_limit_pressure")
dim xesub8 as xmlelement = xmldoc.createelement("t_upper_limit_holding_time")
dim xesub9 as xmlelement = xmldoc.createelement("t_holding_time_lower_limit")
 
xesub1.innertext = pressure_visualization.t_device_address
xesub2.innertext = pressure_visualization.t_equipment_number
xesub3.innertext = pressure_visualization.t_equipment_state
xesub4.innertext = pressure_visualization.t_packing_pressure
xesub5.innertext = pressure_visualization.t_pressure_holding_time
xesub6.innertext = pressure_visualization.t_upper_limit_pressure
xesub7.innertext = pressure_visualization.t_lower_limit_pressure
xesub8.innertext = pressure_visualization.t_upper_limit_holding_time
xesub9.innertext = pressure_visualization.t_holding_time_lower_limit
 
xe1.appendchild(xesub1)
xe1.appendchild(xesub2)
xe1.appendchild(xesub3)
xe1.appendchild(xesub4)
xe1.appendchild(xesub5)
xe1.appendchild(xesub6)
xe1.appendchild(xesub7)
xe1.appendchild(xesub8)
xe1.appendchild(xesub9)
root.appendchild(xe1) '添加到<bookstore>节点中
xmldoc.save(path)
 
 
end sub
''更新更新
private sub pressure_visualizationupdata(pressure_visualization as pressure_visualization)
 
dim path as string
dim xmldoc as new xmldocument()
path = system.io.path.getfullpath("../../../pressure_visualization.xml")
xmldoc.load(path)
dim root as xmlnode = xmldoc.selectsinglenode("pressure_visualizationconfig") '查找<bookstore>
 
dim t_device_address as xmlnode = root.childnodes(0).selectsinglenode("t_device_address") '查找t_device_address
dim t_equipment_number as xmlnode = root.childnodes(0).selectsinglenode("t_equipment_number") '查找t_equipment_number
dim t_equipment_state as xmlnode = root.childnodes(0).selectsinglenode("t_equipment_state") '查找t_equipment_state
dim t_packing_pressure as xmlnode = root.childnodes(0).selectsinglenode("t_packing_pressure") '查找t_packing_pressure
dim t_pressure_holding_time as xmlnode = root.childnodes(0).selectsinglenode("t_pressure_holding_time") '查找t_pressure_holding_time
dim t_upper_limit_pressure as xmlnode = root.childnodes(0).selectsinglenode("t_upper_limit_pressure") '查找t_upper_limit_pressure
dim t_lower_limit_pressure as xmlnode = root.childnodes(0).selectsinglenode("t_lower_limit_pressure") '查找t_lower_limit_pressure
dim t_upper_limit_holding_time as xmlnode = root.childnodes(0).selectsinglenode("t_upper_limit_holding_time") '查找t_upper_limit_holding_time
dim t_holding_time_lower_limit as xmlnode = root.childnodes(0).selectsinglenode("t_holding_time_lower_limit") '查找t_holding_time_lower_limit
t_device_address.innertext = pressure_visualization.t_device_address
t_equipment_number.innertext = pressure_visualization.t_equipment_number
t_equipment_state.innertext = pressure_visualization.t_equipment_state
t_packing_pressure.innertext = pressure_visualization.t_packing_pressure
t_pressure_holding_time.innertext = pressure_visualization.t_pressure_holding_time
t_upper_limit_pressure.innertext = pressure_visualization.t_upper_limit_pressure
t_lower_limit_pressure.innertext = pressure_visualization.t_lower_limit_pressure
t_upper_limit_holding_time.innertext = pressure_visualization.t_upper_limit_holding_time
t_holding_time_lower_limit.innertext = pressure_visualization.t_holding_time_lower_limit
xmldoc.save(path)
 
end sub
 
private function pressure_visualizationread(pressure_visualization as pressure_visualization) as pressure_visualization
 
dim path as string
dim xmldoc as new xmldocument()
path = system.io.path.getfullpath("../../../pressure_visualization.xml")
xmldoc.load(path)
dim root as xmlnode = xmldoc.selectsinglenode("pressure_visualizationconfig") '查找<bookstore>
 
dim t_device_address as xmlnode = root.childnodes(0).selectsinglenode("t_device_address") '查找t_device_address
dim t_equipment_number as xmlnode = root.childnodes(0).selectsinglenode("t_equipment_number") '查找t_equipment_number
dim t_equipment_state as xmlnode = root.childnodes(0).selectsinglenode("t_equipment_state") '查找t_equipment_state
dim t_packing_pressure as xmlnode = root.childnodes(0).selectsinglenode("t_packing_pressure") '查找t_packing_pressure
dim t_pressure_holding_time as xmlnode = root.childnodes(0).selectsinglenode("t_pressure_holding_time") '查找t_pressure_holding_time
dim t_upper_limit_pressure as xmlnode = root.childnodes(0).selectsinglenode("t_upper_limit_pressure") '查找t_upper_limit_pressure
dim t_lower_limit_pressure as xmlnode = root.childnodes(0).selectsinglenode("t_lower_limit_pressure") '查找t_lower_limit_pressure
dim t_upper_limit_holding_time as xmlnode = root.childnodes(0).selectsinglenode("t_upper_limit_holding_time") '查找t_upper_limit_holding_time
dim t_holding_time_lower_limit as xmlnode = root.childnodes(0).selectsinglenode("t_holding_time_lower_limit") '查找t_holding_time_lower_limit
pressure_visualization.t_device_address = t_device_address.innertext
pressure_visualization.t_equipment_number = t_equipment_number.innertext
pressure_visualization.t_equipment_state = t_equipment_state.innertext
pressure_visualization.t_packing_pressure = t_packing_pressure.innertext
pressure_visualization.t_pressure_holding_time = t_pressure_holding_time.innertext
pressure_visualization.t_upper_limit_pressure = t_upper_limit_pressure.innertext
pressure_visualization.t_lower_limit_pressure = t_lower_limit_pressure.innertext
pressure_visualization.t_upper_limit_holding_time = t_upper_limit_holding_time.innertext
pressure_visualization.t_holding_time_lower_limit = t_holding_time_lower_limit.innertext
 
return pressure_visualization
 
end function
 
--c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#--c#c#c#c#c#c#---c#c#c#c#c#c#-----------------------------------------
 
 
 
/// <summary>
/// 创建xml文件
/// </summary>
public void createxmlfile()
{
xdocument xdoc = new xdocument();
//创建根节点
xelement root = new xelement("timeconfig");
xdoc.add(root); //添加根节点到xdoucment对象
xdoc.save(xmlpath); //使用xml的保存会自动在xml文件开始添加:<?xml version="1.0" encoding="utf-8"?>
 
}
 
 
fileinfo fi = new fileinfo(xmlpath);
if (fi.length <57 )
{
createxmlfile();
}
 
 
public int reatch(int id) {
xmldocument xmldoc = new xmldocument();
string path;
path = system.io.path.getfullpath("../../../lighttime.xml");
xmldoc.load(path);
xmlnode xn;
xn = xmldoc.selectsinglenode("timeconfig");
xmlnodelist xnl = xn.childnodes;
xmlnode xnc;
xnc = xn.selectsinglenode("timecon");
int num;
num = 0;
for (int i = 0; i < xn.childnodes.count - 1; i++)
{
if (xnl[i].childnodes[0].innertext ==""+id)
{
num = 1;
}
}
return num;
}
/// <summary>
/// 把id对应的时间添加到xml文件中
/// </summary>
/// <param name="id"></param>
/// <param name="time"></param>
public void addxml(int id,datetime time) {
 
 
 
string path;
xmldocument xmldoc = new xmldocument();
path = system.io.path.getfullpath("../../../lighttime.xml");
xmldoc.load(path);
xmlnode root;
root = xmldoc.selectsinglenode("timeconfig"); /*'查找<bookstore> */
xmlelement xe1;
xe1 = xmldoc.createelement("timecon"); /*'创建一个<book>节点 */
xe1.setattribute("type", "datetime");/*'设置该节点genre属性 */
xmlelement xesub1;
xesub1 = xmldoc.createelement("id");
xesub1.innertext = id.tostring(); /*'设置文本节点 */
xe1.appendchild(xesub1); /*'添加到<book>节点中 */
xmlelement xesub2;
xesub2 = xmldoc.createelement("lasttime");
xesub2.innertext = time.tostring();
xe1.appendchild(xesub2);
root.appendchild(xe1); /*'添加到<bookstore>节点中 */
xmldoc.save(path);
 
 
}
 
read
public string getreadid( ) {
string path;
path = system.io.path.getfullpath("../../../lighttime.xml");
xmldocument xmldoc=new xmldocument();
xmldoc.load(path);
xmlnode xn;
xn = xmldoc.selectsinglenode("timeconfig");
xmlnodelist xnl= xn.childnodes;
xmlnode xnc = xn.selectsinglenode("timecon");
 
string ids="";
for (int i = 0; i < xn.childnodes.count-1; i++)
{
if (convert.todatetime(xn.childnodes[i].childnodes[1].innertext) <= datetime.now)
{
ids+= xn.childnodes[i].childnodes[0].innertext+"-";
}
}
 
 
return ids;
 
}
xml 文件
 
<?xml version="1.0" encoding="utf-8"?>
<!--记录ic卡的信息-->
<recard>
<num>4</num>
<rfid type="ic卡">
<id>5d6ce040</id>
<rfserialnumber>8582705151485666839576784955525300</rfserialnumber>
<mood>1</mood>
</rfid>
<rfid type="ic卡">
<id>5d6ce040</id>
<rfserialnumber>8582705151485666839576784955535700</rfserialnumber>
<mood>2</mood>
</rfid>
<rfid type="ic卡">
<id>5d6ce040</id>
<rfserialnumber>8582705151485666839576784955525300</rfserialnumber>
<mood>1</mood>
</rfid>
</recard>
 
<?xml version="1.0" encoding="utf-8"?>
<pressure_visualizationconfig>
<pressure_visualization type="ic卡">
<t_device_address>77 03</t_device_address>
<t_equipment_number>1</t_equipment_number>
<t_equipment_state>压力值正常</t_equipment_state>
<t_packing_pressure>9.8</t_packing_pressure>
<t_pressure_holding_time>9</t_pressure_holding_time>
<t_upper_limit_pressure>10</t_upper_limit_pressure>
<t_lower_limit_pressure>3</t_lower_limit_pressure>
<t_upper_limit_holding_time>11</t_upper_limit_holding_time>
<t_holding_time_lower_limit>9</t_holding_time_lower_limit>
</pressure_visualization>
</pressure_visualizationconfig>