1

我正在尝试将元素添加到已经存在的节点/父级中,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
   <BitComet Version="1.0">
      <MyShares>
         <bt title="Sound of Music" save_path="C:\Downloads|Sound of Music" task_finished="true" category="software" size="7126369" hash="31e0ded5b561ed698c151e72d5f20d9b75f03b12"/>
      </MyShares>
   </BitComet>

--

TiXmlDocument xml(pf);
if(!xml.LoadFile()) return;

TiXmlElement* root = xml.FirstChildElement("BitComet");

TiXmlElement* parent = xml.FirstChildElement("MyShares")

TiXmlElement* elem = new TiXmlElement( "bt" );
elem->SetAttribute("title", "TiTlE");
elem->SetAttribute("save_path", "C:\\Downloads|Test");
elem->SetAttribute("task_finished", "true");
elem->SetAttribute("category", "software");
elem->SetAttribute("size", 7126369);
elem->SetAttribute("hash", "hash_here");
xml.LinkEndChild( elem );

xml.LinkEndChild( parent );

xml.LinkEndChild( root );

xml.SaveFile(pf);

我不确定发生了什么,但是当它执行那段代码时它会崩溃。

4

1 回答 1

1

我认为您访问<MyShares>标签的调用应该是

TiXmlElement* parent = root -> FirstChildElement("MyShares");

此外,您想要的唯一LinkEndChild电话如下:

parent->LinkEndChild( elem );
于 2012-05-06T19:29:29.577 回答