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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:20:25+00:00 2026-05-13T17:20:25+00:00

I’m working with an xml file like this. <Envelope xmlns=http://schemas.xmlsoap.org/soap/envelope/> <Body> <Element1> <Element2 day=2009-10-18>

  • 0

I’m working with an xml file like this.

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Body>
    <Element1>
      <Element2 day="2009-10-18">
        <Element3 name="Joe">
          <Element4 time="1">
            <Element5 amount="0" price="16.58"/>
            <Element5 amount="1" price="18.58"/>
            <Element5 amount="2" price="20.58"/>
          </Element4>
        </Element3>
        <Element3 name="Fred">
          <Element4 time="5">
            <Element5 amount="1" price="15.41"/>
            <Element5 amount="2" price="16.41"/>
            <Element5 amount="3" price="17.41"/>
            <Element5 amount="4" price="18.41"/>
          </Element4>
        </Element3>
      </Element2>
    </Element1>
  </Body>
</Envelope>

I need to loop through all the Element3 nodes and then loop through all the Element5 nodes to get an output similar to this.

day, name, time, amount, price 
2009-10-18, Joe, 1, 0, 16.58 
2009-10-18, Joe, 1, 1, 18.58
2009-10-18, Joe, 1, 2, 20.58
2009-10-18, Joe, 1, 1, 16.58
2009-10-18, Fred, 5, 0, 15.41 
etc

My linq query to do this looks something like the below which I thought was great until I realized this was only grabbing the first Element5 node it came to.

XDocument doc = XDocument.Load(@"myfile.xml");
        DataContext st = new DataContext();

        var docxml = from c in doc.Elements("Envelope").Elements("Body").Elements("Element1").Elements("Element2").Elements("Element3")
                     select new mytable()
                     {
                         MyKey = Guid.NewGuid(),
                         day = Convert.ToDateTime(c.Parent.Attribute("day").Value)
                         name = c.FirstAttribute.Value,
                         hour = Convert.ToInt32(c.Element("Element4").FirstAttribute.Value),
                         price = Convert.ToDecimal(c.Element("Element4").Element("Element5").Attribute("price").Value),
                         amount = Convert.ToDecimal(c.Element("Element4").Element("Element5").Attribute("amount").Value)

                     };
        st.mytable.InsertAllOnSubmit(docxml);
        st.SubmitChanges();

How can I loop this to include ALL The Element5 nodes?

  • 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-13T17:20:25+00:00Added an answer on May 13, 2026 at 5:20 pm

    Every from-statement is basically a foreach-loop (easy way to think of it).
    Note: I had to change some of the parent relations

    XDocument doc = XDocument.Load(@"myfile.xml");
    DataContext st = new DataContext();
    
    var docxml = from c in doc.Elements("Envelope").Elements("Body").Elements("Element1").Elements("Element2").Elements("Element3").Elements("Element4")
                 from e in c.Elements("Element5")
                 select new mytable()
                 {
                     MyKey = Guid.NewGuid(),
                     day = Convert.ToDateTime(c.Parent.Parent.Attribute("day").Value)
                     name = c.Parent.FirstAttribute.Value,
                     hour = Convert.ToInt32(c.FirstAttribute.Value),
                     price = Convert.ToDecimal(e.Attribute("price").Value),
                             amount = Convert.ToDecimal(e.Attribute("amount").Value)
    
                 };
    
    st.mytable.InsertAllOnSubmit(docxml);
    st.SubmitChanges();
    

    You could make the code a bit more readable by using the let-statement and better names:

    var docxml = from element4 in doc.Elements("Envelope").Elements("Body").Elements("Element1").Elements("Element2").Elements("Element3").Elements("Element4")
                 let element3 = element4.Parent
                 let element2 = element3.Parent
                 from element5 in c.Elements("Element5")
      ...
    

    I can’t test the code, because I’m on my MacBook. Hope this helps.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
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
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6

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.