C# Split xml file into multiple files


below have xml file, in file, need split xml file multiple xml files based on date column value, suppose have 10 records 3 different dates unique date records should go each file . ex here have file 3 dates output should 3 files while each file containing records of unique date data. didn't idea proceed on this, thats reason not posting code.needed urgently please
<xml>  <rootnode>  <childnode>  <date>2012-12-01</date>  <name>sss</name>  </childnode>  <childnode>  <date>2012-12-01</date>  <name>sss</name>  </childnode>  <childnode>  <date>2012-12-02</date>  <name>sss</name>  </childnode>  <childnode>  <date>2012-12-03</date>  <name>sss</name>  </childnode>  </rootnode>  </xml>  

here full code:

using system.xml.linq;  class curentity {     public datetime date;     public string name;     public curentity(datetime _date, string _name)     {         date = _date;         name = _name;     } } static void main(string[] args) {     xelement xmltree = new xelement("xml",                                         new xelement("rootnode",                                             new xelement("childnode",                                                 new xelement("date"),                                                 new xelement("name")                                             )                                         )                                     );      string infilepath = @"c:\temp\1.xml";     string outfilepath = @"c:\temp\1_";     xdocument xmldoc = xdocument.load(infilepath);     list<curentity> lst = xmldoc.element("xml").element("rootnode").elements("childnode")                    .select(element => new curentity(convert.todatetime(element.element("date").value), element.element("name").value))                    .tolist();      var unique = lst.groupby(i => i.date).select(i => i.key);     foreach (datetime dt in unique)     {         list<curentity> curentities = lst.findall(x => x.date == dt);         xelement outxml = new xelement("xml",                                         new xelement("rootnode")                                     );         foreach(curentity ce  in curentities)         {             outxml.element("rootnode").add(new xelement("childnode",                                                  new xelement("date", ce.date.tostring("yyyy-mm-dd")),                                                 new xelement("name", ce.name)                                     )                         );         }         outxml.save(outfilepath + dt.tostring("yyyy-mm-dd") + ".xml");     }     console.writeline("done");     console.readkey(); }





Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'