1

我有一个 .XML 文件,我编写了一个 GUI 包装器来从文件中添加和删除节点。XML 文件如下所示:

<?xml version="1.0" encoding="iso-8859-1"?>
<ApplicationList>
  <Application>
    <AppID>1</AppID>
    <AppName>1</AppName>
    <AppVer>1</AppVer>
    <AppInstallType>1</AppInstallType>
    <AppInstallArgs>1</AppInstallArgs>
    <AppInstallerLocation>1</AppInstallerLocation>
  </Application>
  <Application>
    <AppID>2</AppID>
    <AppName>2</AppName>
    <AppVer>2</AppVer>
    <AppInstallType>2</AppInstallType>
    <AppInstallArgs>2</AppInstallArgs>
    <AppInstallerLocation>2</AppInstallerLocation>
  </Application>
  <Application>
    <AppID>3</AppID>
    <AppName>3</AppName>
    <AppVer>3</AppVer>
    <AppInstallType>3</AppInstallType>
    <AppInstallArgs>3<AppInstallArgs>
    <AppInstallerLocation>3</AppInstallerLocation>
  </Application>
</ApplicationList>

我有一个自定义对象的 ObservableCollection,我正在迭代这些对象并将其属性添加到 .XML 文件中。这是代码。

//Get the XML from the config file
            XDocument document = XDocument.Load(cf.Path);

            //Define the new XML nodes
            XElement applicationNode = new XElement("Application");

            XElement appIDNode = new XElement("AppID");
            XElement appNameNode = new XElement("AppName");
            XElement appVerNode = new XElement("AppVer");
            XElement appInstallTypeNode = new XElement("AppInstallType");
            XElement appInstallArgsNode = new XElement("AppInstallArgs");
            XElement appInstallerLocationNode = new XElement("AppInstallerLocation");

            //Create the tree with the nodes
            applicationNode.Add(appIDNode);
            applicationNode.Add(appNameNode);
            applicationNode.Add(appVerNode);
            applicationNode.Add(appInstallTypeNode);
            applicationNode.Add(appInstallArgsNode);
            applicationNode.Add(appInstallerLocationNode);

            //Get the node that I need to append to and then append my XML to it
            XElement appListNode = document.XPathSelectElement("/ApplicationList");

            //Remove all the nodes first
            appListNode.RemoveAll();

            foreach (Application app in Applist)
            {
                //Set the values for each node
                appIDNode.SetValue(app.AppID);
                appNameNode.SetValue(app.AppName);
                appVerNode.SetValue(app.AppVer);
                appInstallTypeNode.SetValue(app.AppInstallType);
                appInstallArgsNode.SetValue(app.AppInstallArgs);
                appInstallerLocationNode.SetValue(app.AppInstallerLocation);

                //Add nodes to the XML file
                appListNode.Add(applicationNode);
            }

            //Save the xml file
            document.Save(cf.Path);

在我为其所有属性添加属性值为“4”的 Application 对象后,XML 如下所示:

<?xml version="1.0" encoding="iso-8859-1"?>
<ApplicationList>
  <Application>
    <AppID>4</AppID>
    <AppName>4</AppName>
    <AppVer>4</AppVer>
    <AppInstallType>4</AppInstallType>
    <AppInstallArgs>4</AppInstallArgs>
    <AppInstallerLocation>4</AppInstallerLocation>
  </Application>
  <Application>
    <AppID>2</AppID>
    <AppName>2</AppName>
    <AppVer>2</AppVer>
    <AppInstallType>2</AppInstallType>
    <AppInstallArgs>2</AppInstallArgs>
    <AppInstallerLocation>2</AppInstallerLocation>
  </Application>
  <Application>
    <AppID>3</AppID>
    <AppName>3</AppName>
    <AppVer>3</AppVer>
    <AppInstallType>3</AppInstallType>
    <AppInstallArgs>3</AppInstallArgs>
    <AppInstallerLocation>3</AppInstallerLocation>
  </Application>
  <Application>
    <AppID>4</AppID>
    <AppName>4</AppName>
    <AppVer>4</AppVer>
    <AppInstallType>4</AppInstallType>
    <AppInstallArgs>4</AppInstallArgs>
    <AppInstallerLocation>4</AppInstallerLocation>
  </Application>
</ApplicationList>

为什么我的代码用新对象替换树中的第一个节点?

4

0 回答 0