未保存XML子节点

时间:2016-09-02 12:41:27

标签: c# xml

我正在编写一些C#应用程序来进行备份,我在我的XML文件中保存子节点时出现问题,我存储了所有备份详细信息。

文件本身应如下所示:

<backup id="0" totalParts="3" date="0" name="0" description="0">
    <backup_part id="0" mboxId="0" muid="0">HASH</backup_part>
    <backup_part id="1" mboxId="0" muid="0">HASH</backup_part>
    <backup_part id="2" mboxId="0" muid="0">HASH</backup_part>
</backup>

我使用以下命令创建父节点(“backup”):

    public static XmlElement AddNewBackupNode(
        XmlDocument xmlDoc, int backupId, int totalBackupParts, 
        string backupDate, string backupName, string backupDescription)
    {
        xmlDoc.Load(Program.defaultBackupFile);
        XmlElement singleBackup = xmlDoc.CreateElement("backup");

        var reqNumericAttr = new TupleList<int, string>
        {
            { backupId, "id" },
            { totalBackupParts, "totalParts" }
        };

        var reqStringAttr = new TupleList<string, string>
        {
            { backupDate, "date" },
            { backupName, "name" },
            { backupDescription, "description" }
        };

        // TODO: remove one foreach
        foreach (var attr in reqNumericAttr)
        {
            XmlAttribute newAttribute = xmlDoc.CreateAttribute(attr.Item2);
            newAttribute.Value = attr.Item1.ToString();
            singleBackup.Attributes.Append(newAttribute);
        }

        foreach (var attr in reqStringAttr)
        {
            XmlAttribute newAttribute = xmlDoc.CreateAttribute(attr.Item2);
            newAttribute.Value = attr.Item1.ToString();
            singleBackup.Attributes.Append(newAttribute);
        }

        xmlDoc.Save(Program.defaultBackupFile);
        return singleBackup;
    }

那部分正在运作。

然后我想用这个添加几个(尽可能多的)子节点:

    public static void AddNewBackupPart(XmlDocument xmlDoc, XmlElement backup, string fileHash, int backupPartId, int mboxId, int uid)
    {
        XmlElement backupPart = xmlDoc.CreateElement("backup_part");
        backupPart.InnerText = fileHash;

        var reqAttr = new TupleList<int, string>
        {
            { backupPartId, "id" },
            { mboxId, "mboxId" },
            { uid, "uid" }
        };

        foreach(var attr in reqAttr)
        {
            XmlAttribute newAttribute = xmlDoc.CreateAttribute(attr.Item2);
            newAttribute.Value = attr.Item1.ToString();
            backupPart.Attributes.Append(newAttribute);
        }

        backup.AppendChild(backupPart);
        xmlDoc.Save(Program.defaultBackupFile);
    }

并且那个失败,所以将其内容保存到文件(全新的子节点)。

我尝试以不同的方式运行它,例如添加2个或22个子节点,但它总是以与顶部相同的方式完成 - 使用默认内容。

不会显示任何错误或警告。

任何想法我做错了什么?

编辑:这是我调用AddNewBackupPart的地方:

// add new backup row to XML
XmlDocument xmlDoc = XMLparser.LoadBackupsFile(); // TODO: if above can't be done, move that line to XMLparser and Add... method
XmlElement backupRoot = XMLparser.AddNewBackupNode(xmlDoc, backupId, totalBackupParts, backupDate, backupName, backupDescription);

        // handle each file in folder
        int partId = 1;
        foreach (string fileName in FileHandler.GetAllBackupPackages())
        {
            Console.WriteLine("file: {0}", fileName);

            // generate Hash
            string fileHash = FileHandler.GenerateFileChecksum(fileName);
            Console.WriteLine("hash: {0}", fileHash);

            // add single node to XML
            XMLparser.AddNewBackupPart(xmlDoc, backupRoot, fileHash, partId, 000);

            partId++;
        }

1 个答案:

答案 0 :(得分:0)

您的主要问题是在AddNewBackupNode执行此操作:

xmlDoc.Load(Program.defaultBackupFile);
XmlElement singleBackup = xmlDoc.CreateElement("backup");

然后您将所有内容添加到singleBackup,但您永远不会在文档中添加singleBackup

鉴于您无法向XML文档添加多个根元素,您很可能希望将其添加为已加载的XML中元素的子元素,例如:

xmlDoc.DocumentElement.AppendChild(singleBackup);

顺便说一下,我强烈建议您查看LINQ to XML - 它是一个比XmlDocument更好的API,并且可以让您编写比这更容易理解的代码。它可以用更少的代码更具声明性地编写 - 我在下面列举了一个例子,但是由于代码不完整,大多数变量实际来自哪里并不清楚。:

var backupParts = FileHandler.GetAllBackupPackages()
    .Select((fileName, index) => new XElement("backup_part",
        new XAttribute("id", index + 1),
        new XAttribute("mboxId", "?"),
        new XAttribute("uid", "?"),
        FileHandler.GenerateFileChecksum(fileName)));

var backup = new XElement("backup",
    new XAttribute("id", backupId),
    new XAttribute("totalParts", totalBackupParts),
    new XAttribute("date", backupDate),
    new XAttribute("name", backupName),
    new XAttribute("description", backupDescription),
    backupParts);

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

doc.Root.Add(backup);

doc.Save(Program.defaultBackupFile)