Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7806957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:41:58+00:00 2026-06-02T02:41:58+00:00

I’ve create an xml file from a csv file like this private void button2_Click(object

  • 0

I’ve create an xml file from a csv file like this

private void button2_Click(object sender, EventArgs e)
        {
            String[] FileContent = File.ReadAllLines(csvPathFile);
            String XMLNS = "";
            int idCountProduct = 1;
            XElement Inv = new XElement("data",
                from items in FileContent
                let fields = items.Split(';')
                select new XElement("category",
                    new XAttribute("idCategory", fields[0]),
                    new XAttribute("CategoryName", fields[1]),
                    new XElement("products",
                        new XElement("product",
                            new XAttribute("IdProduct", idCountProduct++),
                            new XAttribute("Rif", fields[0]),
                            new XAttribute("ProductName", fields[2]),

                            new XElement("products",
                                new XElement("product",
                                    new XAttribute("IdProduct", idCountProduct++),
                                    new XAttribute("Rif", fields[0]),
                                    new XAttribute("ProductName", fields[3]),
                                    new XElement("products",
                new XElement("product",
                    new XAttribute("IdProduct", idCountProduct++),
                    new XAttribute("Rif", fields[0]),
                    new XAttribute("ProductName", fields[4]));
            File.WriteAllText(xmlPathFile, XMLNS + Inv.ToString());
        }

this is my csv file

1;Beverages;Lemon Juice;;Orange Juice

this is the xml file i want to create

<data>
<category idCategory="1" CategoryName= "Beverages">
    <products>
      <product IdProduct="1" Rif="1" ProductName= "Lemon Juice" />
      <product IdProduct="2" Rif="1" ProductName= "Orange Juice" />
<products/>
<category/>
</data>

and this is the xml file i obtain

<data>
<categories>
<category idCategory="1" CategoryName= "Beverages">
    <products>
      <product IdProduct="1" Rif="1" ProductName= "Lemon Juice" />
      <product IdProduct="2" Rif="1" ProductName= "" />
      <product IdProduct="3" Rif="1" ProductName= "Orange Juice" />
<products/>
<category/>
<categories/>
</data>

How can I avoid adding a product if ProductName is not assigned?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:42:00+00:00Added an answer on June 2, 2026 at 2:42 am

    Add a filter to your LINQ query to filter out empty products:

    where !String.IsNullOrEmpty(fields[4])
    

    just after this:

    let fields = items.Split(';')
    

    Anyway if your condition is so simple you do not even need it and you may filter out entries from the Split instruction itself:

    let fields = items.Split(';', StringSplitOptions.RemoveEmptyEntries)
    

    EDIT
    Your code is pretty fixed so…are you sure you need to use LINQ and LINQ-to-XML? I guess it’ll be even more readable anyway…write this:

            XElement data = new XElement("data");
            XDocument document = new XDocument(data);
    
            int counter = 0;
            foreach (string entry in File.ReadAllLines(csvPath))
            {
                string[] fields = entry.Split(new char[] { ';' },
                                    StringSplitOptions.RemoveEmptyEntries);
    
                XElement category = new XElement("category",
                    new XAttribute("idCategory", fields[0]),
                    new XAttribute("CategoryName", fields[1]));
                data.Add(category);
    
                XElement products = new XElement("products");
                category.Add(products);
    
                for (int i = 2; i < fields.Length; ++i)
                {
                    products.Add(new XElement("product",
                        new XAttribute("IdProduct", counter++),
                        new XAttribute("Rif", fields[0]),
                        new XAttribute("ProductName", fields[i])));
                }
            }
    
            document.Save(xmlPath);
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.