我试图在XML文件中的每个元素之间添加一个空格,但我不知道该怎么做 这就是我所拥有的
if (sfdSave.ShowDialog() == DialogResult.OK)
{
Data data = new Data();
using (XmlWriter writer = XmlWriter.Create(sfdSave.FileName))
{
writer.WriteStartDocument();
writer.WriteStartElement("MyConnection");
writer.WriteElementString("Host", data.Host);
writer.WriteElementString("User", data.User);
writer.WriteElementString("Password", data.Password);
writer.WriteElementString("Port", Convert.ToString(data.Port));
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
这就是我做的
if (sfdSave.ShowDialog() == DialogResult.OK)
{
Data data = new Data();
using (XmlWriter writer = XmlWriter.Create(sfdSave.FileName))
{
writer.WriteStartDocument();
writer.WriteStartElement("MyConnection");
writer.WriteElementString("Host", data.Host);
writer.WriteElementString("","\n");
writer.WriteElementString("User", data.User);
writer.WriteElementString("","\n");
writer.WriteElementString("Password", data.Password);
writer.WriteElementString("","\n");
writer.WriteElementString("Port", Convert.ToString(data.Port));
writer.WriteElementString("","\n");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
该解决方案无效
任何想法?
由于
答案 0 :(得分:0)
如果你尝试添加 
可能有用......
- 抱歉
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试使用XmlSerializer,因此首先需要创建包含所需的所有属性的类,然后使用xmlSerializer.serialize,在这种情况下,它将以良好的布局创建一个xml文件
首先创建名为MyConnection的类
[Serializable]
public class MyConnection
{
public string User {get;set;}
public string password {get;set;}
public string port {get;set;}
public string host{get;set;}
}
然后当你想写这个文件
MyConnection con = new MyConnection {user ="" , password =""};
XmlSerializer serializer = new XmlSerializer (typeof(MyConnection));
using (FileStream stream = new FileStream(filePath,FileMode.Create)
{
serializer.serialize (stream ,con);
}
您可以通过在每个属性上添加
来添加属性[XmlAttribute]