My C# application has method that takes a list of objects (inputList
) as parameter, it creates an XML string using the TextWriter and XMLWriter with the code below and submits to a webservice.
using (TextWriter writer = new Utf8StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(writer, settings))
{
xw.WriteStartElement("submission");
xw.WriteElementString("version", XMLversion);
xw.WriteElementString("user", USER_NAME);
foreach (var obj in inputList)
{
xw.WriteStartElement("node");
xw.WriteElementString("data1", obj.data1.ToString());
xw.WriteElementString("data2", obj.data2.ToString());
xw.WriteElementString("data3", obj.data3.ToString());
xw.WriteElementString("data4", obj.data4.ToString());
xw.WriteEndElement();
}
}
xmlFile = writer.ToString();
}
One of the requirements to to log the submission for each item in the list individual. So I'd like to know if there's a more efficient way to create a string of the XML node within the foreach
loop?
I've considered using the XMLReader with the string afterwards but that's a whole new process and while I know I can create it manually, and am happy to do so, I am open to other suggestions. In essence, I'm looking for an efficient technique to generate a string as illustrated below:
<node>
<data1>obj.data1.ToString()</data1>
<data2>obj.data2.ToString()</data2>
<data3>obj.data3.ToString()</data3>
<data4>obj.data4.ToString()</data4>
</node>