c#對xml的簡單操作


c#對xml有很完善的操作函數,在system.xml命名空間下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Xml;

namespace xml_test
{
    class Program
    {
        static void Main(string[] args)
        {
            //.net的api函數操作xml,創建,修改等。
            string path = @"test.xml";
            XmlDocument xml = new XmlDocument();
            XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(dec);
            XmlElement root = xml.CreateElement("Config");
            //root.InnerText = "1";
            root.SetAttribute("ip", "192.168.0.1");
            root.SetAttribute("port", "5555");
            xml.AppendChild(root);
            XmlElement greenTea = xml.CreateElement("greenTea");
            

            XmlNode nodeIp = xml.CreateElement("ip");
            XmlNode nodePort = xml.CreateElement("port");
            nodeIp.InnerText = "192.168.0.1";
            nodePort.InnerText = "5000";

            XmlElement zmq = xml.CreateElement("Zmq");
            zmq.SetAttribute("ip", "192.168.0.100");
            zmq.SetAttribute("port", "8000");
            greenTea.AppendChild(nodeIp);
            greenTea.AppendChild(nodePort);
            greenTea.AppendChild(zmq);

            XmlElement alc = xml.CreateElement("ALC");
            alc.SetAttribute("timeout", "8000");
            XmlElement isNeed = xml.CreateElement("isNeed");
            isNeed.InnerText = "true";
            alc.AppendChild(isNeed);

            root.AppendChild(greenTea);
            root.AppendChild(alc);
            xml.Save(path);
            Thread.Sleep(500);

            XmlDocument test = new XmlDocument();
            test.Load(path);
            XmlElement testroot = (XmlElement)test.SelectSingleNode("Config");
            XmlNode green = testroot.SelectSingleNode("greenTea");
            XmlNode ip = green.SelectSingleNode("ip");
            XmlNode zmqtest = green.SelectSingleNode("Zmq");
            Console.WriteLine(ip.InnerText);
            Console.WriteLine(testroot.GetAttribute("port"));
            Console.WriteLine(zmqtest.Attributes["port"].Value);

            XmlDocument sync = new XmlDocument();
            sync.Load("synconfig.xml");
            XmlNode config = sync.SelectSingleNode("Config").SelectSingleNode("Scanner");
            Console.WriteLine(config.Attributes["timeout"].Value);
            Console.WriteLine(config.SelectSingleNode("BaudRate").InnerText);
            Console.ReadKey();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<Config ip="192.168.0.1" port="5555">
  <greenTea>
    <ip>192.168.0.1</ip>
    <port>5000</port>
    <Zmq ip="192.168.0.100" port="8000" />
  </greenTea>
  <ALC timeout="8000">
    <isNeed>true</isNeed>
  </ALC>
</Config>
<?xml version="1.0" encoding="utf-8"?><Config>
    <TestMode>1</TestMode>
    <GreenTea timeout="6000">
        <Name>COM3</Name>
        <BaudRate>115200</BaudRate>
        <Parity>0</Parity>
        <DataBits>8</DataBits>
        <StopBits>1</StopBits>
    </GreenTea>
    <Gimbal timeout="10000">
        <ServerIp>192.168.0.69</ServerIp>
        <Port>5000</Port>
        <ZMQRep ip="127.0.0.1" port="8000"/>
    </Gimbal>
    <Scanner timeout="2000">
        <Name>COM2</Name>
        <BaudRate>115200</BaudRate>
        <Parity>0</Parity>
        <DataBits>8</DataBits>
        <StopBits>1</StopBits>
    </Scanner>
    <ALC timeout="6000">
        <ServerIp>192.168.3.63</ServerIp>
        <Port>8899</Port>
    </ALC>
    <TM>
        <ZMQReq ip="127.0.0.1" port="6480"/>
        <ZMQSub ip="127.0.0.1" port="6880"/>
        <ZMQSubSeq ip="127.0.0.1" port="6250"/>
        <ZMQLogSub ip="127.0.0.1" port="5959"/>
        <ZMQSubZlogger ip="127.0.0.1" port="9595" timeout="12000"/>
    </TM>
    <POGOPININFO uselimit="100000" usedtimes="0" name=""/>
    <TECINFO uselimit="100000" usedtimes="0" name=""/>
</Config>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM