user11334779
user11334779

Reputation:

Append List to XML

I am trying to append my list to an XML file.

I have my c# class PasswordSettings which contains some properties:

public class PasswordSettings {
    public string customerRef { get; set; }
    public string node { get; set; }
    public string name { get; set; }
    public string login { get; set; }
    public string password { get; set; }
    public string fileType { get; set; }
}

I have a list of PasswordSettings like this:

public List<PasswordSettings> Logins = new List<PasswordSettings>();

I now add elements to my object and add the object to my list:

PasswordSettings settings = new PasswordSettings();

settings.customerRef = "abc";
settings.name = "test";

Logins.add(settings);

Now I want to add this list to an XML file so I end up with something like:

<PasswordSettings>
    <Logins>
        <customerRef>abc</customerRef>
        <name>test</name>
    </Logins>
</PasswordSettings>

And if I want to add another login, it will append to the XML file, i.e. not replace or overwrite anything, so a new <Logins>

I have tried multiple methods and I either get null pointers or nothings gets written. I suppose the nullpointer could be because the XML file is empty, but I just want it to add this list as an XML structure.

Upvotes: 1

Views: 965

Answers (1)

Frenchy
Frenchy

Reputation: 17037

here is a solution to create xml or add a new record, so after you could adapt following what you want:

        PasswordSettings settings = new PasswordSettings();
        settings.customerRef = "abc";
        settings.name = "test";
        Logins.Add(settings);
        settings = new PasswordSettings();
        settings.customerRef = "def";
        settings.name = "test1";
        Logins.Add(settings);

        foreach (var login in Logins)
        {
            if (!File.Exists(@"e:\Test.xml"))
            {
                XDocument doc =
                    new XDocument(
                        new XElement("PasswordSettings",
                            new XElement("Logins",
                                new XElement("customerRef", login.customerRef),
                                new XElement("name", login.name)
                            )
                        )
                    );

                doc.Save(@"e:\Test.xml");
            }
            else
            {
                XDocument doc = XDocument.Load(@"e:\Test.xml");
                XElement root = doc.Element("PasswordSettings");
                IEnumerable<XElement> rows = root.Descendants("Logins");
                XElement firstRow = rows.First();
                firstRow.AddBeforeSelf(
                    new XElement("Logins",
                        new XElement("customerRef", login.customerRef),
                        new XElement("name", login.name)));
                doc.Save(@"e:\Test.xml");
            }
        }

        }

xml output:

<?xml version="1.0" encoding="utf-8"?>
<PasswordSettings>
  <Logins>
    <customerRef>def</customerRef>
    <name>test1</name>
  </Logins>
  <Logins>
    <customerRef>abc</customerRef>
    <name>test</name>
  </Logins>
</PasswordSettings>

here i add at the beginning of file, if you want to add at the end of file, just do:

                XElement firstRow = rows.Last();
                firstRow.AddAfterSelf(
                    new XElement("Logins",
                        new XElement("customerRef", login.customerRef),
                        new XElement("name", login.name)));

output:

<?xml version="1.0" encoding="utf-8"?>
<PasswordSettings>
  <Logins>
    <customerRef>abc</customerRef>
    <name>test</name>
  </Logins>
  <Logins>
    <customerRef>def</customerRef>
    <name>test1</name>
  </Logins>
</PasswordSettings>

Upvotes: 1

Related Questions