String^ s =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" +
"<!-- Created 26-04-2013 09:05 -->\r\n" +
"<DEVICE_CONFIG>\r\n" +
" <ALL\r\n" +
" username=\"bob\"\r\n" +
" features=\"all\"\r\n" +
" />\r\n" +
"</DEVICE_CONFIG>";
XElement^ root = XElement::Parse(s);
// The DEVICE_CONFIG node is boring. Skip to the "ALL" node.
XElement^ allNode = dynamic_cast<XElement^>(root->FirstNode);
Dictionary<String^, String^>^ results = gcnew Dictionary<String^, String^>();
for each(XAttribute^ attribute in allNode->Attributes())
{
results->Add(attribute->Name->ToString(), attribute->Value);
}
我只是跳到根的内部元素。您可以通过其他方式找到“ALL”节点:迭代直到找到一个HasAttributes的 XElement,您可以按name搜索“ALL”等。