c#保留字符串第一个空格,去除所有剩下的空格

c#保留字符串第一个空格,去除所有剩下的空格

csharp 复制代码
static void Main(string[] args)
{
	string input = "download 1 , 1";

    // 寻找第一个空格的位置
    int firstSpaceIndex = input.IndexOf(' ');

    // 去除所有空格
    input = input.Replace(" ", string.Empty).Insert(firstSpaceIndex, " ");

    Console.WriteLine(input);

    Console.ReadLine();
}

保存数据到xml文件

csharp 复制代码
public void submit()
{
    List<string> AllPorts = new List<string>(){
    "1,2",
    "1,3",
    "1,4",
    "1,5"
    };
    //创建一个数据集,将其写入xml文件
    string optime = ConfigFile + ".xml";
    string path = Environment.CurrentDirectory + "\\config\\" + optime;
    //ds.WriteXml(path);
    XDocument xdoc = new XDocument();
    //为文档增加一文档声明
    XDeclaration xdecl = new XDeclaration("1.0", "utf-8", null);
    //创建一个根节点
    XElement xelement = new XElement("List");

    //把根节点添加到文档中

    xdoc.Add(xelement);
    XElement xConfig = new XElement("Config");

    //把根节点添加到文档中
    xConfig.SetElementValue("ID", DateTime.Now.ToString("yyyyMMddHHmmss"));
    xConfig.SetElementValue("StartFrequency", "100"); 
    xConfig.SetElementValue("StopFrequency", "1690");
    xelement.Add(xConfig);
    
    for (int i = 0; i < AllPorts.Count; i++)
    {
        //为根节点下添加子节点
        XElement xperson = new XElement("PortsConfig");
        //为节点添加属性
        //xperson.SetAttributeValue("id", (i + 1).ToString());
        //给子节点添加文本节点
        xperson.SetElementValue("Ports", AllPorts[i]);
        //添加到根节点下

        xelement.Add(xperson);
    }
    //保存Xml文件

    xdoc.Save(path);

    MessageBox.Show("数据已成功保存到"+ ConfigFile + "XML 文件!", "提示");
    //DataSet ds2 = new System.Data.DataSet();
    //ds2.ReadXml("D:\\"+ConfigFile + ".xml");
    //DataTable dt = new DataTable();
    //dt = ds2.Tables[0];
    AllPorts.Clear();
}

读取xml数据

xml数据

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<List>
  <Config>
    <ID>20240223162658</ID>
    <StartFrequency>100</StartFrequency>
    <StopFrequency>1690</StopFrequency>
  </Config>
  <PortsConfig>
    <Ports>1,2</Ports>
  </PortsConfig>
  <PortsConfig>
    <Ports>1,3</Ports>
  </PortsConfig>
  <PortsConfig>
    <Ports>1,4</Ports>
  </PortsConfig>
  <PortsConfig>
    <Ports>1,5</Ports>
  </PortsConfig>
</List>

读取xml

csharp 复制代码
static void Main(string[] args)
{
    DataSet ds = new DataSet();
    ds.ReadXml(@"F:\项目\\bin\Debug\config\112233.xml");
    DataTable dt = ds.Tables[0];
    DataTable dt2 = ds.Tables[1];
    string[] ports = null;
    for (int i = 0; i < dt2.Rows.Count; i++)
    {
        ports = dt2.Rows[i][0].ToString().Split(',');

        Console.WriteLine(ports[0] + "," + ports[1]);
    }
}
相关推荐
隐积1 天前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
名字还没想好☜1 天前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏1 天前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
海盗12341 天前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
酷在前行1 天前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8281 天前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
废弃的小码农1 天前
功能测试--Day07--Python编程基础
开发语言·python
fengci.1 天前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
程序员zgh1 天前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
念何架构之路1 天前
restartmanager-重启管理子系统
java·开发语言