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

  • Home
  • SEARCH
  • 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 776239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:25:00+00:00 2026-05-14T19:25:00+00:00

I want by linq save my xml in csv and I have o problem.

  • 0

I want by linq save my xml in csv and I have o problem.

This bracket are here beacuse without it this code is not displaying (why ? )

<results>
    <Countries country="Albania">
        <Regions region="Centralna Albania">
            <Provinces province="Durres i okolice">
                <Cities city="Durres" cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760">
                    <IndividualFlagsWithForObjects Status="1" />
                    <IndividualFlagsWithForObjects  Status="0" />
                    <IndividualFlagsWithForObjects magazyn="2" />
                </Cities>
            </Provinces>
        </Regions>
    </Countries>
    <Countries country="Albania">
        <Regions region="Centralna Albania">
            <Provinces province="Durres i okolice">
                <Cities city="Durres" cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760">
                    <IndividualFlagsWithForObjects storage="0" Status="1" /> 
                    <IndividualFlagsWithForObjects storage="1" Status="0" /> 
                    <IndividualFlagsWithForObjects storage="2" Status="1" /> 
                </Cities>
            </Provinces>
        </Regions>
    </Countries>
</results>

I must point one important thing:
the parent node is
but when I use it loaded.Descendants(“results”) it gives me nothing.

XDocument loaded = XDocument.Load(@"c:\citiesxml.xml");

// create a writer and open the file
TextWriter tw = new StreamWriter("c:\\XmltoCSV.txt");

// Query the data and write out a subset of contacts
var contacts = (from c in loaded.Descendants("Countries")
    select new
    {
        Country = (string)c.Attribute("ountry").Value,
        Region = (string)c.Element("Regions").Attribute("region").Value,
        Province= c.Element("Regions").Element("Provinces").Attribute("prowincja").Value,
        City= c.Element("Regions").Element("Provinces").Element("Cities").Attribute("city").Value,
        Kod = c.Element("Regions").Element("Provinces").Element("Cities").Attribute("cityCode").Value,
        IndywidualnaFlagaStatus = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("Status"),
        IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")
    }).ToList();

last problem:

 IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")

gives me :

IndywidualnaFlagaWartosc = {storage="0"} (I see this while debugging)
  • 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-05-14T19:25:00+00:00Added an answer on May 14, 2026 at 7:25 pm
    var contacts = (from c in loaded.Descendants("Countries")
                            select new
                            {
                                Country = (string)c.Attribute("Country").Value,
                                Region = (string)c.Element("Regions").Attribute("region").Value,
                                Province = (string)c.Element("Regions").Element("Provinces").Attribute("province").Value,
                                City = (string)c.Element("Regions").Element("Provinces").Element("Cities").Attribute("city").Value,
                                Hotel = (string)c.Element("Hotels").Attribute("hotel").Value
                            }).ToList();
    

    Hotel is not in your xml anywhere so that will need to be adjusted. I would generally recommend that you pull each item once and check for nulls, instead of pulling Regions 3 times as I have done here.

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

Sidebar

Related Questions

I have written a LINQ to XML query that does what I want, but
I want to make a deep copy of a LINQ to XML XElement. The
I have a DataGridView that I want to query using Linq (C# WinForm). I
I have a working LINQ to SQL model. I want to be able to
i`ve got a little problem with LINQ. I read out some information via XML-RPC.
I need LINQ to grab a whole table, but this seems not to be
I have a List of X items. I want to have LINQ query that
I'm working with Linq to xml, and I save my xml in a standars
I am a beginner in using LINQ. I have an xml data with the
i am stuck with this small problem in my code. I am trying to

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.