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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:36:44+00:00 2026-06-12T17:36:44+00:00

I am a .net beginner. I need to add some data to xml file

  • 0

I am a .net beginner. I need to add some data to xml file

the xml file is:

<stock>    --- 1st level  /* i dont want to create this because this exists */ 
  <items>  --  2nd level
    <productname>Toothpaste</productname>
    <brandname>Colgate</brandname>
    <quantity>12</quantity>
    <price>10</price>
  </items>
  <items>
    <productname>Toothpaste</productname>
    <brandname>Pepsodent</brandname>
    <quantity>20</quantity>
    <price>12</price>
  </items>
</stock>

I need to add

productname --> Toothpaste
brandname   --> CloseUp
quantity    --> 16
price       --> 15

to their respective tags. The problem I am facing now is that I need to go two levels deep to write to their respective tags, which i dont know how to do.

I tried the below code: (not working)

XDocument doc = new XDocument(      
                  new XElement("stock",  /* how to go inside existing "stock"? */
                     new XElement("items", 
                          new XElement("productname", "Toothpaste"),
                          new XElement("brandname", "CloseUp"),
                          new XElement("quantity","16"),
                          new XElement("price","15"))));

There must be some other way to achieve this which I dont know.

Answers not related to linq are also welcome. but more preference to linq because I have implemented full linq in my project.

Please Help

Thanks in Advance.

  • 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-12T17:36:45+00:00Added an answer on June 12, 2026 at 5:36 pm

    Assuming you have the original document:

     var doc = XDocument.Load(...);
    

    then create a new element (not a document)

      //XDocument doc = new XDocument(      
      //  new XElement("stock",  /* how to go inside existing "stock"? */
       var newElement =  new XElement("items", 
                          new XElement("productname", "Toothpaste"),
                          new XElement("brandname", "CloseUp"),
                          new XElement("quantity","16"),
                          new XElement("price","15"));
    

    And then insert it:

      doc.Element("stock").Add(newElement);
    
      doc.Save(....);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginner in WinForms VB NET programming. I need to create text box
This is a beginner level question for asp.net MVC I have the following code
I am a .net beginner. I am reading a XML file and showing it
I’m beginner for ASP.NET MVC , so I need to functioning create and edit
I am just a beginner in ASP.NET. My question is simple, I wanna add
I am a complete beginner to ASP.NET and need to know how do I
I need codes for vb.net Getkeystate Function() without using any DLL.I'm beginner for API
Good Morning everybody. I'm a beginner in .Net languages and need an example to
I am a beginner to .NET, I have some doubts in my mind. Can
I am beginner to asp.net . i need to alert the currently logged in

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.