1

I'm trying to add a space between each element in a XML file but I have no idea how to do it This is what I have

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();
            }
        }

and this is what I did

        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();
            }
        }

That solution is not working

any idea?

Thanks

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
Javier Salas
  • 1,064
  • 5
  • 15
  • 38

3 Answers3

0

how about if you try adding  

that might work..

-- Sorry about that

psj01
  • 3,075
  • 6
  • 32
  • 63
0

I think you should have another method that "beautifies" your xml string.

Found this in a similar post maybe it can help.

Adding a helper to XmlDocument

Community
  • 1
  • 1
0

try to use XmlSerializer so you need first to create class contains the all properties that you need and then use xmlSerializer.serialize in this case it will create an xml file in good layout

first create class named 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;}
}

then when you want to write this file

MyConnection con = new MyConnection {user ="" , password =""};
XmlSerializer serializer = new XmlSerializer (typeof(MyConnection));
using (FileStream stream = new FileStream(filePath,FileMode.Create)
{
serializer.serialize (stream ,con);
}

you can add attribute by adding above each property

[XmlAttribute]
j.kahil
  • 276
  • 5
  • 16