简体   繁体   中英

c# XDocument.Root.Add not adding any element

I have an XML that stores my project data (Projects.xml) and its structure is like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<projects xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:\Users\tima\Desktop\ProjectSchema.xsd">
  
  <project>
    <wpcode>00-01</wpcode>
    <wpname>New Office Floor Construction</wpname>
  </project>

  <project>
    <wpcode>00-02</wpcode>
    <wpname>Office Ground Floor Refurbushing</wpname>
  </project>

  <project>
    <wpcode>00-03</wpcode>
    <wpname>Warehouse Utilities</wpname>
  </project>

  <project>
    <wpcode>00-04</wpcode>
    <wpname>Warehouse Security Upgrade</wpname>
  </project>

  <project>
    <wpcode>00-05</wpcode>
    <wpname>Warehouse Racks Installation</wpname>
  </project>
  
</projects>

I am trying to add new project to this xml but the code isn't adding any element to this file.

My code for adding the new element to the xml file is:

static class Program
{
    static public string AppRoot
    {
        get
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }
    }
}

public static class AppDataConfig
{
    /// <summary>
    /// The application root folder of the data files.
    /// </summary>
    public static string DataFilePath = "Data Files";
}

public static class ProjectXmlEngine
{
    static string fileName = "Projects";
    static string fileType = "xml";
    public static XNamespace ns = ProjectsDataFile.Root.GetDefaultNamespace();
    public static XName tagProject = ns + "project";
    public static XName tagProjCode = ns + "wpcode";
    public static XName tagProjName = ns + "wpname";

    /// <summary>
    /// The XML document containing all projects data.
    /// </summary>
    public static XDocument ProjectsDataFile
    {
        get
        {
            string fullPath;
            fullPath = GetProjectsFilePath();
            return File.Exists(fullPath) ? XDocument.Load(fullPath) : null;
        }
    }

    static string GetProjectsFilePath()
    {
        return Program.AppRoot + AppDataConfig.DataFilePath
            + @"\" + fileName + "." + fileType;
    }

    public static bool AddProject(Project whichProject)
    {
        XElement project =
            new XElement(tagProject,
                new XElement(tagProjCode, "00-06"/*whichProject.wProjectCode*/),
                new XElement(tagProjName, "sample data"/*whichProject.ProjectName*/));
        
        ProjectsDataFile.Root.Add(project);
        string filePath = @"D:\Programing\Visual Studio 2013\Projects\Procurment System\Procurment Application\bin\Debug\Data Files\Projects (Saved).xml";
        ProjectsDataFile.Save(filePath);
        
        return true;
    }
}

When I run the code the generated file is the same as the source file and the code is not adding the new element I am entering.

I made another application with code similar to this one and the XML are the same but with different element names but the same structure and the code is working I even tried removing the Namespace from the XML document but the result is the same.

What am I doing wrong in my code ?

Thanks

The problem is that with every access to ProjectsDataFile , the file is loaded from disk.

That's here:

ProjectsDataFile.Root.Add(project);

and here:

ProjectsDataFile.Save(filePath);

In detail:

a) you load the file from disk, you add a node to it, but never use the result. The XDocument that you got, will be garbage collected immediately.

b) you load the file from disk again and immediately save it somewhere else.

What you should do instead: keep the one file you loaded and continue operating on it.

var file = ProjectsDataFile; file.Root.Add(project); string filePath = @"..."; file.Save(filePath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM