0

I have the data in the list as below.

List<APIConfigValue> 

Now I have to generate the XML file as below.

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ScanTime.Web" value="11D_SVC1" />
    <add key="eTime.Punch" value="true" />
    <add key="DataMartUrl" value="syscontrol" />
  </appSettings>
</configuration>

Based on the no of records in the list, it should automatically generate. I am trying using xml linq libraries.

This is how i started...

XDocument doc = new XDocument(
            new XElement("configuration",
                new XElement("appSettings",
                    new XElement("add", new XAttribute("key", "APP.Web"), new XAttribute("value", "true"))
                    )
            )
        );

I got stuck on how to loop through from the list here. I tried to find articles. But nothing close. Appreciate your time and Responses.

2
  • You should maybe use the XmlSerializer class instead of the XDocument. Commented Aug 13, 2015 at 19:44
  • How does APIConfigValue look like? Commented Aug 13, 2015 at 19:48

2 Answers 2

1

I am going to assume that each APIConfigValue has two properties: key and value.

List<APIConfigValue> data = foo;
XDocument doc = new XDocument(
    new XElement("configuration",
        new XElement("appSettings",
            data.Select((conf) => new XElement("add", new XAttribute("key", conf.key), new XAttribute("value", conf.value)).ToArray()
        )
    )
);

What this does is map each APIConfigValue to an XElement with two properties: the key and value of the APIConfigValue.

However, if you plan on both exporting and importing this XML format I would recommend taking a look at the XMLSerializer class.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you've populated your List accordingly:

List<APIConfigValue> yourList = //assign your list here;
foreach (APIConfigValue apiConfigItem in yourList)
{
    //write out your nodes from the list here using apiConfigItem
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.