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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:17:37+00:00 2026-05-26T14:17:37+00:00

I’m creating a new XDocument and inserting a root element profiles in it, then

  • 0

I’m creating a new XDocument and inserting a root element “profiles” in it, then saving.

if (!System.IO.File.Exists("profiles.xml"))
{
    XDocument doc = new XDocument(
        new XElement("profiles")
    );
    doc.Save("profiles.xml", SaveOptions.None);
}

And then later I wanna take users input and add profiles into the already created xml file:

XElement profile =
        new XElement(Player.Name,
            new XElement("level", Player.Level),
            new XElement("cash", Player.Cash)
        );

XDocument doc = XDocument.Load("profiles.xml");
List<XElement> profiles = doc.Root.Elements().ToList();

for (int i = 0; i < profiles.Count; i++)
{
    if (profiles[i].Name.ToString() == Player.name)
    {
        profiles[i] = profile;
        return;
    }
}
profile.Add(profile);
doc.Save("profiles.xml", SaveOptions.None);

But for some reason, it will never add any new profiles?

EDIT: Also, if I manually create a new profile into the xml file, it won’t customize either, so the problem is within Saving the file?

  • 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-26T14:17:38+00:00Added an answer on May 26, 2026 at 2:17 pm

    You’re never actually doing anything to change any of the elements within the XDocument that doc refers to:

    • If you find an element with the existing name, you’re modifying the list, but that won’t modify the document. You probably want to use XElement.ReplaceWith:

      profiles[i].ReplaceWith(profile);
      

      Note that in this case you’re not even trying to save the XML file again (due to the return statement), so it’s not really clear what you’re trying to achieve in this case.

    • If you don’t find the element, you’re adding the profile element to itself, which certainly isn’t going to modify the document. I suspect you want:

      doc.Root.Add(profile);
      

      In other words, add the new profile element as a new final child of the root element.

    EDIT: Here’s a different approach to try instead – I’m assuming any one name should only occur once:

    XDocument doc = XDocument.Load("profiles.xml");
    var existingElement = doc.Root
                             .Elements()
                             .Where(x => x.Name.ToString() == Player.name)
                             .FirstOrDefault();
    if (existingElement != null)
    {
        existingElement.ReplaceWith(profile);
    }
    else
    {
        doc.Root.Add(profile);
    }
    doc.Save("profiles.xml", SaveOptions.None);
    

    Also, I would strongly advise you not to use the player name as the element name. Use it as an attribute value or text value instead, e.g.

    XElement profile =
            new XElement("player",
                new XAttribute("name", Player.Name),
                new Attribute("level", Player.Level),
                new XAttribute("cash", Player.Cash)
            );
    

    That way you won’t have problems if the player name has spaces etc. You’d then need to change your query to:

    var existingElement = doc.Root
                             .Elements()
                             .Where(x => (string) x.Attribute("name)" == Player.name)
                             .FirstOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
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'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

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.