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」節點:迭代,直到找到一個XElement,您可以通過name等搜索「ALL」。
您能否提供更多示例輸入?正因爲如此,很難說出什麼是可以預期和依賴的恆定標籤,什麼是需要忽略的可變數據。 – 2013-05-02 15:01:58
這是更好嗎?我提供的輸入是整個文件,除了所有的鍵值對。我預計每個文件大約有30個鍵值對。 – Stefan 2013-05-02 16:20:23
我想要更多的鍵值對。只用你顯示的數據,不清楚什麼是關鍵,什麼是價值,什麼是垃圾不包含在輸出中。你是否期待字典中包含「username = bob」,「features = all」,而不是「bob = all」? – 2013-05-02 18:38:32