首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从XML模板更新XML文件

从XML模板更新XML文件
EN

Stack Overflow用户
提问于 2013-07-20 03:31:23
回答 1查看 139关注 0票数 0

我有两个xml文件,一个叫做config.xml和configtemplate.xml。我尝试做的是向config.xml文件中添加来自configtemplate.xml的行。

代码语言:javascript
复制
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中的值将与它们可能具有的值不同。

代码语言:javascript
复制
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>

我希望我的解释是正确的,谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-07-20 03:36:35

如果我没理解错的话,您可以使用ImportNode将数据从一个xml添加到另一个xml。

就像这样。

代码语言:javascript
复制
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);
}

这样,它就可以将configtemplate.xml<Database><Services><Options>导入到<config>下的config.xml

在您的示例中,您需要知道要导入的源和目标标签,这在您的示例中没有指定。

Explanation:

doc1rootconfig.xml的根标签,即<config>

doc2root<config>configtemplate.xml,的根标签

foreach循环中,遍历doc2root的每个子节点(即<Database><Services><Options>),并将每个子节点添加为doc1root的子节点

使用上面的代码,您将获得一个新的config.xml,如下所示。

代码语言:javascript
复制
<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-->

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17754069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档