从XML模板更新XML文件
Update XML file from a XML Template
我有两个xml文件,一个名为config.xml和configtemplate.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>
我希望我自己解释得正确,谢谢
如果我理解正确,您可以使用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>
、<Options>
从configtemplate.xml导入<config>
下的configuration.xml
在您的情况下,您需要知道要从和导入到的标记,该标记在您的示例中没有指定。
解释:
doc1root
是config.xml的根标签,即<config>
doc2root
是configtemplate.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-->
相关文章:
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- c++ 读取 xml 文件的内容
- 删除 XML 文件中的子节点C++
- 在Cmake构建CmakeList.txt中读取/解析XML文件
- 使用 libxml2 解析 XML 文件时缺少元素值
- 为什么TinyXML2的XMLDocument::FirstChild()函数在尝试解析这个有效的XML文件时返回NULL?
- 如何循环 n 次,在 xml 文件中每个循环一个级别
- 无法从 XML 文件中获取值
- 如何让用户在 tinyxml2 中输入 XML 文件的路径
- 使用 TinyXML2 读取 C++ 中的 XML 文件
- OPENCV - 如何在OpenCV中使用Haar Cascade Github xml文件?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- 如何使用QXmlStreamReader解析包含对其他XML文件的引用的XML文件
- 如何解析列存储格式的XML文件
- OpenCV - 打开一个从C++到Python的XML文件
- 修改 XML 文件 QXMLStreamReader/Writer
- 如何使用libcurl c ++从url保存XML文件
- C++/RapidXML:编辑节点并写入新的 XML 文件没有更新的节点
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件
- 是否可以将信息从XML文件转换为CPP