1

ファイルからノードを追加および削除するための GUI ラッパーを作成した .XML ファイルがあります。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