0

config.xml と configtemplate.xml という名前の 2 つの xml ファイルがあります。私がやろうとしているのは、configtemplate.xml から config.xml ファイルに行を追加することです。

config.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
 <Database>
    <DataType>1</DataType>
    <ServerName>192.168.50.80</ServerName>
  // add information here supplied by the configtemplate.xml
 </Database>

 <Services>
    <Wash>1</Wash>
  // add information here supplied by the configtemplate.xml
 </Services>

 <Options>
    <TaxRate>8.125</TaxRate>
    <AskForZipcode>0</AskForZipcode>
 // add information here supplied by the configtemplate.xml
 </Options>

私が必要とするのは、configtemplate.xml からすべてのデータを取得し、それを上書きせずに構成ファイルに追加し、それらの値を設定することです。

また、configtemplate.xml の値は、それらが持つ可能性があるものとは異なります。

configtemplate.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
 <Database>
    <DataType>1</DataType>
    <ServerName>192.168.50.80</ServerName>
  // add all lines below to config.xml
    <DatabaseName>TestDB</DatabaseName>
 </Database>

 <Services>
    <Wash>1</Wash>
  // add all lines below to config.xmlxml
    <Greeter>0</Greeter>
 </Services>

 <Options>
    <TaxRate>8.125</TaxRate>
    <AskForZipcode>0</AskForZipcode>
 // add all lines below to config.xml
    <AutoSave>1</AutoSave>
 </Options>

自分で正しく説明していることを願っています。ありがとうございます

4

1 に答える 1

0

私があなたを正しく理解していればImportNode、ある xml から別の xml にデータを追加するために使用できます。

このようなもの。

XmlDocument doc1 = new XmlDocument();
doc1.Load("config.xml");
XmlDocument doc2 = new XmlDocument();
doc2.Load("configtemplate.xml");
XmlNode doc1root, doc2root, importNode;

doc1root = doc1.DocumentElement;
doc2root = doc2.DocumentElement;

foreach(XmlNode node in doc2root.ChildNodes)
{
    importNode = doc1root.OwnerDocument.ImportNode(node, true);
    doc1root.AppendChild(importNode);
}

このようにして<Database><Services>configtemplate.xml<Options>からconfig.xmlにインポートします。<config>

あなたの場合、インポート元およびインポート先のタグを知る必要がありますが、これは例では指定されていません。

説明:

doc1rootconfig.xmlのルート タグです。<config>

doc2rootconfigtemplate.xmlのルート タグです。<config>

foreachループでは、( 、 、および ) の各子ノードをループし、各子ノードを の子にdoc2root追加します。<Database><Services><Options>doc1root

上記のコードを使用すると、次のような新しいconfig.xmlが得られます。

<config>
 <Database>
    <DataType>1</DataType>
    <ServerName>192.168.50.80</ServerName>
    // add information here supplied by the configtemplate.xml
 </Database>

 <Services>
    <Wash>1</Wash>
    // add information here supplied by the configtemplate.xml
 </Services>

 <Options>
   <TaxRate>8.125</TaxRate>
   <AskForZipcode>0</AskForZipcode>
   // add information here supplied by the configtemplate.xml
 </Options>

 //below are from configtemplate.xml
 <Database>
    <DataType>1</DataType>
    <ServerName>192.168.50.80</ServerName>
    // add all lines below to config.xml
    <DatabaseName>TestDB</DatabaseName>
 </Database>

 <Services>
    <Wash>1</Wash>
    // add all lines below to config.xmlxml
    <Greeter>0</Greeter>
 </Services>

 <Options>
    <TaxRate>8.125</TaxRate>
    <AskForZipcode>0</AskForZipcode>
    // add all lines below to config.xml
    <AutoSave>1</AutoSave>
 </Options>
</config>

ちなみに、xml のコメントの正しい付け方は html と同じで、<!--comment-->

于 2013-07-19T19:36:35.490 に答える