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 8993793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:16:26+00:00 2026-06-15T23:16:26+00:00

My problem is that the store name should be the sub element of company

  • 0

My problem is that the store name should be the sub element of company name but what happen is that they are the same in line How can I fix this?

Here is the sample output:

    <?xml version="1.0" encoding="utf-8"?>
<Records>
  <CompanyName>Company 1</CompanyName>
  <StoreName>Store 2</StoreName>
  <StoreName>Store 3</StoreName>
  <StoreName>Store 5</StoreName>
  <CompanyName>Company 2</CompanyName>
  <StoreName>Store 1</StoreName>
  <StoreName>Store 4</StoreName>
</Records>

Here is my code:

qlCommand cmd = new SqlCommand("select company_name, company_id from company", cn);
            //SqlCommand cmd = new SqlCommand("SELECT dbo.company.company_name, dbo.store.store_name, dbo.store.company_id FROM dbo.company INNER JOIN dbo.store ON dbo.company.company_id = dbo.store.company_id", cn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);

            SqlCommand cmd2 = new SqlCommand("select store_name, company_id from store", cn);
            SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2);
            DataTable dt2 = new DataTable();
            adapter2.Fill(dt2);

            dataGridView1.DataSource = dt;

            XElement xml = new XElement("Records");
            foreach (DataRow row in dt.Rows)
            {
                xml.Add(new XElement("CompanyName", row["company_name"].ToString()));
                foreach (DataRow row1 in dt2.Rows)
                {
                    if(row["company_id"].ToString() == row1["company_id"].ToString())
                    {
                        xml.Add(new XElement("StoreName", row1["store_name"].ToString()));
                    }
                }
            }

            xml.Save("C:\\Users\\PHWS13\\Desktop\\test.xml");
  • 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-15T23:16:27+00:00Added an answer on June 15, 2026 at 11:16 pm

    The problem is that you are adding the element to xml which is always at the same level (i.e. you are always adding elements as children of your “Records” element).

    Try this:

    XElement xml = new XElement("Records");
    foreach (DataRow row in dt.Rows)
    {
        var companyName = new XElement("CompanyName", row["company_name"].ToString())
        xml.Add(companyName);
        foreach (DataRow row1 in dt2.Rows)
        {
            if(row["company_id"].ToString() == row1["company_id"].ToString())
            {
                companyName.Add(new XElement("StoreName", row1["store_name"].ToString()));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store data, the problem is that I only know the name
Simple problem that I can't figure out... How can I print a '%' character
Seems to be a problem that many people have, but all the answers I
I'm facing a problem that I can summarize as it follows: I have a
I have the same problem that is listed in the following thread. WSDL first
I'm having a problem that only seems to happen on my Lenovo Thinkpad Tablet
Have a problem that seems easy on paper but i'm having a big problem
can you help me with my problem: i have ASP.NET web site that have
Sorry for the disturbance but can you help me in my problem? I'm new
I have finally stumbled across a problem that I can't already find the answer

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.