1 Replies - 1200 Views - Last Post: 26 October 2010 - 09:53 AM Rate Topic: -----

#1 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,446
  • Joined: 07-April 08

Creating XElement from csv file

Posted 25 October 2010 - 02:22 PM

So i'm converting csv's to XML to better transform and combine them. I'm running into an issue with it however.

I want the XML to look like so:
<root>
  <shop id="1234">
   <detail></detail>
   <detail></detail>
   <detail></detail>
   <detail></detail>
   <detail></detail>
  </shop>
</root>



but i can't seem to figure out how to accomplish this.

Here is the code i'm using:
*NOTE: this code was done based off of this MSDN Article, and was pretty much adapted to attempt what i needed.
public static XElement ConvertUSAToXml(string filepath,string filename)
    {
      string[] source = File.ReadAllLines(filepath);
      XElement xElement = new XElement("Shop",
        new XElement("FileName",filename),
        from str in source
        let fields = str.Split(',')
        where fields[0] != "ID"
        select new XElement("Job",  //<-- I don't want this sub element i want everything to be a child Element of Shop
          new XElement("ID",fields[0]),
          new XElement("Field",fields[1]),
          new XElement("Field",fields[2]),
          new XElement("Field",fields[3]),
          new XElement("Field",fields[4]),
          new XElement("Field",fields[5]),
          new XElement("Field",fields[6]),
          new XElement("Field",fields[7]),
          new XElement("Field",fields[8]),
          new XElement("Field",fields[9]),
          new XElement("Field",fields[10]),
          new XElement("Field",fields[11]),
          new XElement("Field",fields[12]),
          new XElement("Field",fields[13]),
          new XElement("Field",fields[14]),
          new XElement("Field",fields[15]),
          new XElement("Field",fields[145]),
          new XElement("Field",fields[151]),
          new XElement("Field",fields[152])
        )
      );
      return xElement;


    }



This post has been edited by rgfirefly24: 25 October 2010 - 04:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating XElement from csv file

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,446
  • Joined: 07-April 08

Re: Creating XElement from csv file

Posted 26 October 2010 - 09:53 AM

Partial Answer. Because the above code is returning a XElement collection you cannot just explicitly return it as a XElement. In order to have just a single Parent element and have either Attributes of that Element or Children to that Element you have to use the .FirstOrDefault() of Linq to return just the first element of the collection. So my code turned out to be this:

public static XElement ConvertUSAToXml(string filepath,string filename)  
{  

  string[] source = File.ReadAllLines(filepath);  
  XElement xElement = (from str in source
        let fields = str.Split(',')
        where fields[0] != "ID"
        select new XElement("Shop",10           
             new XElement("ID",fields[0]),  
             new XElement("Field",fields[1]),  
             new XElement("Field",fields[2]),  
             new XElement("Field",fields[3]),  
             new XElement("Field",fields[4]),  
             new XElement("Field",fields[5]),  
             new XElement("Field",fields[6]),  
             new XElement("Field",fields[7]),  
             new XElement("Field",fields[8]),  
             new XElement("Field",fields[9]),  
             new XElement("Field",fields[10]),  
             new XElement("Field",fields[11]),  
             new XElement("Field",fields[12]),  
             new XElement("Field",fields[13]),  
             new XElement("Field",fields[14]),  
             new XElement("Field",fields[15]),  
             new XElement("Field",fields[145]),  
             new XElement("Field",fields[151]),  
             new XElement("Field",fields[152])  
         )  
       ).FirstOrDefault();  

       return xElement;  
} 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1