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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:56:27+00:00 2026-06-14T11:56:27+00:00

I have a problem, and i cant figure out what is the matter. I

  • 0

I have a problem, and i cant figure out what is the matter. I want to insert elements to my XML file from a listbox. In the listbox there are Menuelem elements, which has a string and an int variable.

    dt = DateTime.Now;
    XDocument doc = XDocument.Load(path);

    XElement user = new XElement("user", new XAttribute("id", id),
                        new XElement("order", new XAttribute("id", key),
                            new XElement("date", dt.ToString()))
                    );
    doc.Element("orders").Add(user);
    doc.Save(path);
    foreach (Menuelem item in listBox6.Items)
    {
        int j=0;
        var menuelem = new XElement("menuelem", new XAttribute("db", j),
                       new XElement("name", item.Nev),
                       new XElement("price", item.Ar));
        doc.Element("order").Add(menuelem); //throws nullreferenceexception
        doc.Save(path);
        j++;
    }

    listBox6.Items.Clear();
    label3.Text = "";
    key++;

}

What I want to look my XML is like this:

<?xml version="1.0" encoding="utf-8" ?> 
<orders>
 <user id="0">
  <order id="0">
   <date>2012.11.19. 2:16:12</date>
   <menuelem db = "0">
    <name>asdasdas</name>
    <price>1000</price>
   <menuelem db = "1">
    <name>asds</name>
    <price>2000</price>
  </order>
 <user id="0">
  <order id="1">
    <date>2012.11.19. 2:16:15</date>
    <menuelem db = "0">
      <name>asdasdas</name>
      <price>1000</price>
    <menuelem db = "1">
      <name>asds</name>
      <price>2000</price>
   </order>
 </user>
</orders>

Can anyone solve this using linq?

  • 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-14T11:56:29+00:00Added an answer on June 14, 2026 at 11:56 am

    You code tries to find “order” element as immediate child of the document which contains only root element (“orders”) and as result getting null. See Element for details on what it searches for elements and when returns null.

    You need to correctly select your newly inserted element. Simple option is to use Descendants by name and select last one (which is the user just added by Add)

     doc.Descendants("order").Last().Add(menuelem);
    

    Easier option is to build “user” element completely and than add it to the tree. Than you don’t need to search document for nodes just added to it.

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

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
I have a very similar problem, but i cant seem to figure it out,
I have a weird problem that i can't figure out a solution for: I've
I have a problem with Lucene's scoring function that I can't figure out. So
Hy, I have a problem and I can't figure it out... declare @start_date datetime,
I have this code and can't figure out what seems to be the problem:
I have a small problem which i can't seem to figure out. I tried
I have a seemingly simple problem, but I can't quite figure out a solution.
I have a pretty basic problem but I can't seem to figure it out...
So I have a problem that I can not figure out. I am writing

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.