c#保留字符串第一个空格,去除所有剩下的空格
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文件
public void submit()
{
List<string> AllPorts = new List<string>(){
"1,2",
"1,3",
"1,4",
"1,5"
};
string optime = ConfigFile + ".xml";
string path = Environment.CurrentDirectory + "\\config\\" + optime;
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.SetElementValue("Ports", AllPorts[i]);
xelement.Add(xperson);
}
xdoc.Save(path);
MessageBox.Show("数据已成功保存到"+ ConfigFile + "XML 文件!", "提示");
AllPorts.Clear();
}
读取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
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]);
}
}