zoukankan      html  css  js  c++  java
  • 将字符串以用二进制流的形式读入XML文件

        其实将字符串写入XML文件本身并不复杂,这里只是写一些需要注意的地方,特别是编码格式,这里需要的是XML默认的编码方式是UTF-8,在对字符串进行编码的时候一定要注意,

        string strReceiveData = srs.ReceiveData();
    
        byte[] data = Encoding.UTF8.GetBytes(strReceiveData);   

          将当前的字符串保存到XML文件中

       System.IO.FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "VedioChannel.xml", FileMode.Create);
    
        BinaryWriter bw = new BinaryWriter(stream);
        bw.Write(data);   

          但是这样的写入方式,没有任何格式,全都乱作一团,但是在读取的时候也是没有问题的。

          另外一种方式是采用System.Xml命名空间下面的一些Load方法、XmlTextWriter的一些方法来完成写入,这种方法能够设置字符串的一些缩进格式等,这样整理出来的XML格式更加工整,而且采用了一种完全不同的方式,这两种方式都能达到效果。

     //将当前的xml形式的的string 写入到XML文件
        public void WriteStringToXml(string recvString)
        {
            //首先清除掉XML文件里面的所有内容
           XmlDocument xd = new XmlDocument();
           try
             {
                 xd.Load(System.AppDomain.CurrentDomain.BaseDirectory + "VedioChannel.xml");
                 xd.LoadXml(recvString);
    
                 XmlTextWriter writer = new XmlTextWriter("VedioChannel.xml", null);
                 writer.Formatting = Formatting.Indented;//设置非常重要的缩进格式
                 xd.Save(writer);
             }
          catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //xd.Save(System.AppDomain.CurrentDomain.BaseDirectory + "VedioChannel.xml");
    
          }
    

      

       

  • 相关阅读:
    js正则 转载
    asp.net中打开新窗口的多种方法(转载)
    ajax有两种提交数据的方式,分别为get和post(转)
    DropDownList 绑定(转载)
    CentOS网络配置
    Java内存区域-- 运行时数据区域
    Spring Ioc--Bean装配
    Spring--Spring容器
    java正则表达式 --简单认识
    Leetcode 402:移除K个数字
  • 原文地址:https://www.cnblogs.com/seekdream/p/4498636.html
Copyright © 2011-2022 走看看