Bnaffy
Bnaffy

Reputation: 129

Save an xml file with the value of a node

I have a file that contains a CSV entry that I am using to create XML. Right now I am saving it with a generic name, but what I would like to do is use the value of a node or a combination of multiple nodes as the name of the xml file so it is unique.

For example,

C:\LOGGER 20150119\013521sa.au,Line01,20150119,013521,value,Yes,No,Out,7652816686,1,something,2220

and I would like the save the file as

C:/Line01_value.xml.

Would thise be possible? This is what I am using to create the xml right now:

string[] lines = File.ReadAllLines(@"C:\file.csv");
        XElement xml = new XElement("root",
            from str in lines
            let columns = str.Split(',')
            select new XElement("recording_info",
                new XElement("recorded_accound_id", columns[1]),//Line01
                new XElement("date_created_ts", String.Format("{0:####-##-##  ##:##:##}", Convert.ToInt64(columns[2] + columns[3]))),     //date                               
                new XElement("recorded_cid", columns[9]),//1
                 new XElement("recording_tag", columns[1]),//Line01
                new XElement("from_caller_id", columns[10] + "  <"+columns[8]+ ">")//ball memorial H



                ) );
        xml.Save(@"C:\XMLFile.xml");<<I want to change this..

EDIT: What all of this is for is I have a CSV with multiple csv entries and I need to create an XML for each entry and save it using values from the xml/csv so it would be unique

Upvotes: 1

Views: 128

Answers (1)

Arie
Arie

Reputation: 5373

for each line in your file, use new object of anonymous type to get your filename and an XElement to save (I removed 'root' node here), then save the files:

string[] lines = File.ReadAllLines(@"C:\file.csv");

var xmls = (from str in lines
                let columns = str.Split(',')
                select new
                {
                     XFILENAME = columns[1] + "_" + columns[4],
                     XELEM = new XElement("recording_info",
                         new XElement("recorded_accound_id", columns[1]),//Line01
                         new XElement("date_created_ts", String.Format("{0:####-##-##  ##:##:##}", Convert.ToInt64(columns[2] + columns[3]))),     //date                               
                         new XElement("recorded_cid", columns[9]),//1
                         new XElement("recording_tag", columns[1]),//Line01
                         new XElement("from_caller_id", columns[10] + "  <" + columns[8] + ">"))
                }).ToList();

            xmls.ForEach(a => a.XELEM.Save(@"C:\" + a.XFILENAME + ".xml"));

or use a dictionary:

var xmls2 = (from str in lines
                         let columns = str.Split(',')
                         select new KeyValuePair<string, XElement>(
                             columns[1] + "_" + columns[4],
                             new XElement("recording_info",
                                 new XElement("recorded_accound_id", columns[1]),//Line01
                                 new XElement("date_created_ts", String.Format("{0:####-##-##  ##:##:##}", Convert.ToInt64(columns[2] + columns[3]))),     //date                               
                                 new XElement("recorded_cid", columns[9]),//1
                                 new XElement("recording_tag", columns[1]),//Line01
                                 new XElement("from_caller_id", columns[10] + "  <" + columns[8] + ">"))
                         )).ToDictionary(a => a.Key, a => a.Value);

foreach (var o in xmls2)
{
     o.Value.Save(@"C:\" + o.Key + ".xml");
}

Upvotes: 2

Related Questions