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]);
    }
}
相关推荐
DogDaoDao1 小时前
Windows 开发提效工具全景指南:60+ 工具的工程化分层配置
windows·git·程序员·开发工具·powershell·everything·msys2
luj_17681 小时前
大律师考核应重能力与科技素养
c语言·开发语言·c++·经验分享·算法
leonkay2 小时前
C# 特性(Attribute)——【1】基础讲解
开发语言·青少年编程·面试·c#·.net·个人开发
harmful_sheep3 小时前
java group by常见用法
java·开发语言·python
SKH.3 小时前
Linux软件编程(6)线程间通信
java·开发语言·数据库
whcyhhh3 小时前
头歌实践教学平台:数据科学与大数据技术导论(十六)
大数据·开发语言·python
萌动的小火苗3 小时前
数据结构面试题【无答案】
c语言·开发语言·数据结构·链表
宸津-代码粉碎机3 小时前
AI工程化高阶实战|从Demo到生产落地核心架构、全套源码与避坑指南
java·大数据·开发语言·人工智能·python·架构
CSDN_RTKLIB3 小时前
数据库七大符号表
c#
XR1234567884 小时前
办公网自主创新建设怎么选?信创办公场景的选型逻辑
开发语言·php