zoukankan      html  css  js  c++  java
  • xml 文件操作

    '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>

     

     
     
     
     
     
     
     
  • 相关阅读:
    DirectX标准规定 DirectX和OpenGL的不同
    Android 抽屉效果的导航菜单实现
    Servlet基础(三) Servlet的多线程同步问题
    Java微服务之Spring Boot on Docker
    Spring Cloud 微服务架构学习笔记与示例
    从你的全世界路过—一群程序员的稻城亚丁游记
    从一个国内普通开发者的视角谈谈Sitecore
    吴军《硅谷来信》思维导图笔记
    .NET Core微服务之基于Jenkins+Docker实现持续部署(Part 1)
    2018OKR年中回顾
  • 原文地址:https://www.cnblogs.com/lantenglc/p/11670565.html
Copyright © 2011-2022 走看看