c#将一个xml文件内的节点复制到另一个xml文件内
1 调用方式:
read_write("c:\\child.xml", "c:\\parent.xml");
2 函数实现:
using System.Xml;
public
void read_write(string xml_in_file, string xml_out_file)
{
try
{
//in xml
XmlDocument doc_in = new XmlDocument();
doc_in.Load(xml_in_file);
if (!doc_in.HasChildNodes)
{
return;
}
XmlNodeList root_node_in =
doc_in.getElementsByTagName_r("object");//节点名字肯定都有
XmlNode xml_in = root_node_in.Item(0);
XmlElement xe_in = (XmlElement)xml_in;
string str_in_id = xe_in.GetAttribute("id");
//out xml
XmlDocument doc_out = new XmlDocument();
doc_out.Load(xml_out_file);
if (!doc_out.HasChildNodes)
{
return;
}
XmlNodeList root_node_out =
doc_out.getElementsByTagName_r("object_set");
XmlNode xml_out = root_node_out.Item(0);
XmlElement xe_out = (XmlElement)xml_out;
string str_out_id = xe_out.GetAttribute("id");
XmlElement xe_del = (XmlElement)xml_out.ChildNodes[0];//
xe_out;
//如果有该节点,先删除,再插入,比修改方便
DeleteNode(xe_out.ChildNodes,"","id",
xe_in.GetAttribute("id"));
XmlNode new_node = doc_out.ImportNode(xe_in, true);
xe_out.A(new_node);